【问题标题】:Bitmap image is null after resize调整大小后位图图像为空
【发布时间】:2012-07-28 16:58:06
【问题描述】:

我正在尝试将从 SD 卡中选择的图片调整为缩略图大小,但是当我调整它的大小时,我的默认图像尺寸 128/128 位图为空

InputStream input=null;
            try {
                input = getActivity().getContentResolver().openInputStream(Uri.parse(cursor.getString(3)));
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(input, null, options);
                int height = options.outHeight; //1030
                int width = options.outWidth; //1033
                BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_picture, options);

                int imageHeight = options.outHeight; //128
                int imageWidth = options.outWidth; //128


                if(imageWidth > imageHeight){
                    options.inSampleSize = Math.round((float)height/(float)imageHeight);
                }else{
                    options.inSampleSize = Math.round((float)width/(float)imageWidth);
                }
                options.inJustDecodeBounds = false;
                Bitmap bm = BitmapFactory.decodeStream(input,null, options); //null here
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                image.setImageBitmap(bm);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

我可以同时获得图像的高度和宽度,但是当我调整它的大小时,它返回一个空位图。

我已经用这种方式处理资源文件,但从来没有使用 SD 卡中图像的Uri,所以我做错了什么?

【问题讨论】:

    标签: android bitmap


    【解决方案1】:

    您不能两次使用相同的输入流,因此您需要第二次创建一个新的 InputStream。请参阅here 了解更多信息。

    【讨论】:

    • 啊不知道,原来是这个原因