Android 尝试加载缩小版本时位图为空

Android 尝试加载缩小版本时位图为空,android,Android,我正在尝试缩小位图的比例,以便将较小的版本加载到内存中。除了从图像库而不是资源中加载外,我基本上遵循谷歌的示例(高效地搜索加载大型位图)。但在计算尺寸后,我似乎得到了一个空位图。这是我的密码: /** OnActivityResult Method **/ final Uri imageUri = data.getData(); final InputStream imageStream = getActivity().getContentResolver().openInput

我正在尝试缩小位图的比例,以便将较小的版本加载到内存中。除了从图像库而不是资源中加载外,我基本上遵循谷歌的示例(高效地搜索加载大型位图)。但在计算尺寸后,我似乎得到了一个空位图。这是我的密码:

/** 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);
        }

有人能发现我做错了什么吗?

我设法让它工作起来了。感谢Biraj Zalavadia()提供的缩放逻辑和这里的光标代码()。这是我的onActivityResult():


您是否设法获取uri并打开输入流?(这意味着:final Uri imageUri=data.getData();final InputStream imageStream=getActivity().getContentResolver().openInputStream(imageUri)中没有空值或异常;?在“真正”读取输入流之前,需要重置输入流
   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
    }