0

I'm attempting to scale down a bitmap to load a smaller version into memory. I'm pretty much following Google's example (search Loading Large Bitmaps Efficiently), except that I'm loading from the image gallery instead of a resource. But I seem to be getting back a null bitmap after calculating dimensions. Here's my code:

/** OnActivityResult Method **/
    final Uri imageUri = data.getData();
    final InputStream imageStream = getActivity().getContentResolver().openInputStream(imageUri);
    Bitmap bitmapToLoad = Util.decodeSampledBitmapFromResource(imageStream, 500, 500); // bitmapToLoad is null.

    mIvScreenshot.setImageBitmap(bitmapToLoad);


/**Helper Methods **/
    public static int calculateInSampleSize(
                BitmapFactory.Options options, int reqWidth, int reqHeight) {
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;

            if (height > reqHeight || width > reqWidth) {

                final int halfHeight = height / 2;
                final int halfWidth = width / 2;

                // Calculate the largest inSampleSize value that is a power of 2 and keeps both
                // height and width larger than the requested height and width.
                while ((halfHeight / inSampleSize) >= reqHeight
                        && (halfWidth / inSampleSize) >= reqWidth) {
                    inSampleSize *= 2;
                }
            }

            return inSampleSize;
        }

        public static Bitmap decodeSampledBitmapFromResource(InputStream is,
                                                             int reqWidth, int reqHeight) {
            Rect rect = new Rect();

            // First decode with inJustDecodeBounds = true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(is, rect, options);

            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeStream(is, rect, options);
        }

Can anyone catch what I'm doing wrong?

jwBurnside
  • 849
  • 4
  • 31
  • 66
  • Do you manage to get an uri and open the input stream? (Meaning no nulls nor exceptions in: final Uri imageUri = data.getData(); final InputStream imageStream = getActivity().getContentResolver().openInputStream(imageUri); ? – Juan Jun 14 '17 at 15:59
  • 1
    https://stackoverflow.com/a/13872663/833647 You need to reset the inputstream before you read it "for real" – Ken Wolf Jun 14 '17 at 16:07

1 Answers1

0

I managed to get it working. Thanks to Biraj Zalavadia (How to reduce an Image file size before uploading to a server) for the scaling logic, and the cursor code here (How to return workable filepath?). Here is my onActivityResult():

   try {
        final Uri imageUri = data.getData();

        String[] filePath = { MediaStore.Images.Media.DATA };
        Cursor cursor = getActivity().getContentResolver().query(imageUri, filePath, null, null, null);
        cursor.moveToFirst();
        String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

        Uri newUri = Uri.parse(ScalingUtilities.scaleFileAndSaveToTmp(imagePath, 500, 500));

        final Bitmap selectedImage = BitmapFactory.decodeFile(newUri.getEncodedPath());
        mIvScreenshot.setImageBitmap(selectedImage);
    } catch (Exception e) {
        // Handle
    }
jwBurnside
  • 849
  • 4
  • 31
  • 66