2

I'm going to create attachments in my android application. I need to attach images. Here is my code:

...
attachButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(Intent.createChooser(intent, "Select image"), CHOOSE_IMAGE);
        }
    });
...


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == CHOOSE_IMAGE) {
        if(resultCode == RESULT_OK) {
            Uri uri = data.getData();
            ImageView imageView = new ImageView(this);

            Bitmap bitmap = getDecodedImageFromUri(uri);
            imageView.setImageBitmap(bitmap);
         }
    }
 }

 private Bitmap getDecodedImageFromUri(Uri uri) {
    InputStream inputStream = null;
    try {
        inputStream = getContentResolver().openInputStream(uri);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    Rect rect = new Rect(0, 0, 0, 0);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    BitmapFactory.decodeStream(inputStream, rect, options);
    options.inSampleSize = getInSampleSize(options, 128, 128);

    options.inJustDecodeBounds = false;
    Bitmap bitmap = BitmapFactory.decodeStream(inputStream, rect, options); //HERE IS PROBLEM - bitmap = null.
    return bitmap;
}

private int getInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    int width = options.outWidth;
    int height = options.outHeight;
    int inSampleSize = 1;

    if(height > reqHeight || width > reqWidth) {
        int halfHeight = height / 2;
        int halfWidth = width / 2;

        while ((halfHeight / inSampleSize) > reqHeight &&
                (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

I add comment where is problem. So, I did debug, and on this moment:

 Bitmap bitmap = BitmapFactory.decodeStream(inputStream, rect, options);

bitmap equals to null.

What's problem? What I do wrong?

As you can see, these helper methods are from android developer's guide.

UPDATED

I need to decode two times, because I need to get options and then get image size to calculate InSampleSize to zip this image.

On second time, options not equals to null - I check it via debug.

But, after second decoding options has outWidth and outHeight as -1. So, it set to default. I don't know what happens on this moment.

4 Answers 4

1

I guess your problem may be because the calling to decodeStream twice

BitmapFactory.decodeStream(inputStream, rect, options); //HERE
options.inSampleSize = getInSampleSize(options, 128, 128);

options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, rect, options); //HERE AGAIN
return bitmap;
3
  • setting the stream position back to the start may fix it. Or get a new stream. As the origional one will have been consumed.
    – IAmGroot
    Mar 30, 2016 at 15:35
  • Yeah, I will try to save original stream, and when I will decode it on second time, will use that copy of original. But, I need to use it twice for setting options, because, I also need to crop image and get it less weight (not 2 mb, 200 kb, for example).
    – DefaultXYZ
    Mar 30, 2016 at 20:22
  • Yes, I get InputStream twice, and it helps me! Thank you a lot!
    – DefaultXYZ
    Mar 31, 2016 at 7:54
0

You can try BitmapFactory.decodeStream(inputStream, null, options);

1
  • can you please add some more information to your answer - at least the inital variables for inputstream and options and make sure to explain why you use null as parameter! thanks!
    – cramopy
    Mar 30, 2016 at 16:31
0

From the docs, the default constructor of the BitmapFactory.Options may be giving you this troubl:

BitmapFactory.Options() Create a default Options object, which if left unchanged will give the same result from the decoder as if null were passed.

And if the options paramter is null (or behaving as null) then the result will be null:

The decoded bitmap, or null if the image data could not be decoded, or, if opts is non-null, if opts requested only the size be returned (in opts.outWidth and opts.outHeight)

Try making a better options object or using the method

public static Bitmap decodeStream (InputStream is)
0

What If you try this way:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK)
    {
        Uri imageUri = data.getData();
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
    }
}
1
  • Unfortunately, it doesn't help me, and I don't know why :(
    – DefaultXYZ
    Mar 30, 2016 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.