1

我正在尝试将从 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();
            }

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

我已经用资源文件这样做了,但从来没有使用UriSD 卡中的图像,所以我做错了什么?

4

1 回答 1

0

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

于 2012-07-31T00:19:47.880 回答