1

I am trying to resize a picture selected from the SD card to a thumbnail size but when I resize it, to the dimensions on my default image 128/128 the bitmap is null

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();
            }

I can get both of the images height and width but when I go to resize it, it returns a null bitmap.

I have done it this way with resource files but never with a Uri of an image from the SD card so what am I doing wrong?

tyczj
  • 71,600
  • 54
  • 194
  • 296

1 Answers1

0

You can't use the same input stream twice, so you will need to create a new InputStream the second time. See here for more info.

Community
  • 1
  • 1
maxko87
  • 2,892
  • 4
  • 28
  • 43