Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 选项outHeight和outWidth对于大型图像返回-1_Android_Bitmap - Fatal编程技术网

Android 选项outHeight和outWidth对于大型图像返回-1

Android 选项outHeight和outWidth对于大型图像返回-1,android,bitmap,Android,Bitmap,我正在尝试调整大图像的大小。对于小图像,此代码可以正常工作,但是对于大图像,outHeight和outWidth始终为-1,这会创建NullPointerException。这特别奇怪,因为我设置了options.inJustDecodeBounds=true,这应该可以防止这种错误,因为内存使用减少了。我错过什么了吗?我的方法如下: @Override protected void onActivityResult(int requestCode, int resultCode,

我正在尝试调整大图像的大小。对于小图像,此代码可以正常工作,但是对于大图像,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;
    }
@覆盖
ActivityResult上受保护的void(int请求代码、int结果代码、,
意图(返回内容){
super.onActivityResult(请求代码、结果代码、图像返回内容);
if(requestCode==REQUEST\u CODE&&resultCode==RESULT\u OK){
Uri selectedImage=imageReturnedIntent.getData();
BitmapFactory.Options=new-BitmapFactory.Options();
options.inJustDecodeBounds=true;
InputStream InputStream=null;
试一试{
inputStream=getContentResolver().openInputStream(
选择图像);
}捕获(FileNotFoundException e1){
Log.d(“onActivityResult”、“FileNotFoundException”);
e1.printStackTrace();
}
解码流(inputStream,null,选项);
试一试{
inputStream.close();
}捕获(IOE1异常){
Log.d(“onActivityResult”、“IOException”);
e1.printStackTrace();
}
options.inJustDecodeBounds=false;
options.inSampleSize=计算样本大小(选项、图像大小、,
图像大小);
InputStream inputStreamTwo=null;
试一试{
inputStreamTwo=getContentResolver().openInputStream(
选择图像);
}catch(filenotfounde异常){
Log.d(“onActivityResult”、“FileNotFoundException”);
e、 printStackTrace();
}
位图unscaledImage=BitmapFactory.decodeStream(InputStream2,
空,选项);
image=Bitmap.createScaledBitmap(未缩放图像、图像大小、,
图像大小,错误);
Log.d(“未缩放图像字节计数”,
字节数:“+unscaledImage.getByteCount());
Log.d(“缩放图像字节数”,“字节数:”+image.getByteCount());
}
}
公共静态int-calculateInSampleSize(BitmapFactory.Options、,
输入要求宽度,输入要求高度){
//图像的原始高度和宽度
最终内部高度=options.outHeight;
最终整数宽度=options.outWidth;
int inSampleSize=1;
如果(高度>要求高度| |宽度>要求宽度){
//计算高度和宽度与所需高度和宽度的比率
最终整数高度比=数学圆((浮动)高度
/(浮动)高度);
最终整数宽度比=数学圆((浮动)宽度/(浮动)宽度);
//选择最小比率作为采样值,这将保证
//最终图像的两个尺寸均大于或等于
//要求的高度和宽度。
inSampleSize=高度比<宽度比?高度比:宽度比;
}
返回样本大小;
}
谢谢你的帮助


更新:这也发生在小图像上(大约100KB),它们是JPG。手机摄像头拍摄的更大的JPG(1.2MB)不会出现这种情况。

问题是图像来自Picasa,因此它们没有存储在SD上,从而产生了NullPointerException。您可以在启动活动之前设置intent.putExtra(intent.EXTRA\u LOCAL\u ONLY,true),将库中的项目仅限于SD卡上的项目。如果我找到一个更好的解决方案,我会发布