Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>this works fine for me taken from the Hack Book from facebook </p> <pre><code>public static Bitmap scaleImage(final Context context, final Uri photoUri) throws IOException { InputStream is = context.getContentResolver().openInputStream(photoUri); final BitmapFactory.Options dbo = new BitmapFactory.Options(); dbo.inJustDecodeBounds = true; BitmapFactory.decodeStream(is, null, dbo); is.close(); int rotatedWidth, rotatedHeight; final int orientation = getOrientation(context, photoUri); if (orientation == 90 || orientation == 270) { rotatedWidth = dbo.outHeight; rotatedHeight = dbo.outWidth; } else { rotatedWidth = dbo.outWidth; rotatedHeight = dbo.outHeight; } Bitmap srcBitmap; is = context.getContentResolver().openInputStream(photoUri); if (rotatedWidth &gt; MAX_IMAGE_DIMENSION || rotatedHeight &gt; MAX_IMAGE_DIMENSION) { final float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION); final float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION); final float maxRatio = Math.max(widthRatio, heightRatio); final BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = (int) maxRatio; srcBitmap = BitmapFactory.decodeStream(is, null, options); } else { srcBitmap = BitmapFactory.decodeStream(is); } is.close(); if (orientation &gt; 0) { final Matrix matrix = new Matrix(); matrix.postRotate(orientation); srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(), matrix, true); } final String type = context.getContentResolver().getType(photoUri); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (type.equals("image/png")) { srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); } else if (type.equals("image/jpg") || type.equals("image/jpeg")) { srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); } final byte[] bMapArray = baos.toByteArray(); baos.close(); return BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length); } public static int getOrientation(final Context context, final Uri photoUri) { /* it's on the external media. */ final Cursor cursor = context.getContentResolver().query(photoUri, new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null); if (cursor.getCount() != 1) { return -1; } cursor.moveToFirst(); return cursor.getInt(0); } </code></pre> <p>in your <code>onActivityResult</code> just call <code>scaleImage(this,YOU_URI);</code> and <code>imageView.setImageBitmap(Your_Bitmap);</code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload