简体   繁体   中英

How to get bitmap from file manager?

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.

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;

你可以试试BitmapFactory.decodeStream(inputStream, null, options);

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM