BitmapFactory.Options outHeight和outWidth为大图像返回-1

时间:2013-07-29 22:46:27

标签: android bitmap

我正在尝试调整大图片的大小。对于小图像,此代码可以正常工作,但是对于大图像,outHeight和outWidth始终为-1,从而产生NullPointerException。这是特别奇怪,因为我设置options.inJustDecodeBounds = true,这应该可以防止这种错误,因为内存使用减少了。我错过了什么吗?我的方法如下:

   @Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            Uri selectedImage = imageReturnedIntent.getData();
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            InputStream inputStream = null;
            try {
                inputStream = getContentResolver().openInputStream(
                        selectedImage);
            } catch (FileNotFoundException e1) {
                Log.d("onActivityResult", "FileNotFoundException");
                e1.printStackTrace();
            }
            BitmapFactory.decodeStream(inputStream, null, options);
            try {
                inputStream.close();
            } catch (IOException e1) {
                Log.d("onActivityResult", "IOException");
                e1.printStackTrace();
            }
            options.inJustDecodeBounds = false;
            options.inSampleSize = calculateInSampleSize(options, IMAGE_SIZE,
                    IMAGE_SIZE);
            InputStream inputStreamTwo = null;
            try {
                inputStreamTwo = getContentResolver().openInputStream(
                        selectedImage);
            } catch (FileNotFoundException e) {
                Log.d("onActivityResult", "FileNotFoundException");
                e.printStackTrace();
            }
            Bitmap unscaledImage = BitmapFactory.decodeStream(inputStreamTwo,
                    null, options);
            image = Bitmap.createScaledBitmap(unscaledImage, IMAGE_SIZE,
                    IMAGE_SIZE, false);
            Log.d("Unscaled image byte count",
                    "Bytes: " + unscaledImage.getByteCount());
            Log.d("Scaled image byte count", "Bytes: " + image.getByteCount());
        }
    }

    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) {

            // Calculate ratios of height and width to requested height and width
            final int heightRatio = Math.round((float) height
                    / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }

        return inSampleSize;
    }

感谢您的帮助!

更新:这也发生在JPG上的小图像(大约100KB)上。对于手机相机拍摄的较大JPG(1.2MB),情况并非如此。

1 个答案:

答案 0 :(得分:0)

问题是图片来自Picasa,所以它们没有存储在SD上,因此会产生NullPointerException。您可以在启动活动之前设置intent.putExtra(Intent.EXTRA_LOCAL_ONLY,true),以将Gallery中的项目仅限制为SD卡上的项目。如果找到一个,我会发布一个更好的解决方案。