user5448913
user5448913

Reputation:

How to get a Bitmap from an ImageView

I wish to get a Bitmap from an ImageView loaded with Glide like such:

Glide.with(getContext()).load(URL)
            .thumbnail(0.5f)
            .crossFade()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(my_imageView);

I have tried the following:

imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();

and

BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();

And none of them has worked for me so far.

How can this be achieved?

Upvotes: 12

Views: 22722

Answers (4)

SMBiggs
SMBiggs

Reputation: 11688

Things have changed, and the answers here need updating. So here's my solution in kotlin. This is simplified, no synthetics or view binding (hehe).

    val imageView : ImageView = findViewById(R.id.my_imageview)
    val bitmap : Bitmap = imageView.drawable.toBitmap()

You need to make sure that the ImageView has had time to draw before doing this. onViewCreated() in a Fragment or after a click event is a good time to do this. The overhead shouldn't be too much.

Upvotes: 4

aQwus jargon
aQwus jargon

Reputation: 133

presently setDrawingCacheEnabled has been deprecated, so another solution is using

ImageView imageView = findViewById(R.id.image);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

Or to obtain Bitmap from Uri, we can use the Glide library.

Bitmap bitmap = Glide.with(this) //taking as bitmap
                     .load(uri //Uri)
                     .asBitmap()
                     .into(100, 100) //width and height
                     .get();

Using Glide is quite good for handling bitmaps. Refer the docs at Handling Bitmaps

Upvotes: 2

spydon
spydon

Reputation: 11512

I had the problem that the bitmap still always resulted in null (even though using the drawing cache, probably some race with the Facebook ShareButton/ShareContent) so I had the following solution to make sure that glide was done loading the image:

Add a listener to glide

Glide.with(this)
        .load(url)
        .listener(listener)
        .into(imageView);

The Listener

private RequestListener listener = new RequestListener() {
    ...

    @Override
    public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {

        Bitmap bitmap = ((BitmapDrawable) resource).getBitmap();
        return false;
    }
};

Upvotes: 3

Nilesh Singh
Nilesh Singh

Reputation: 1750

Oh, this is a simple error. You need to add this before the code where you are trying to get Bitmap out of the ImageView:

imageView.setDrawingCacheEnabled(true);

In order to get the Bitmap out of the ImageView using DrawingCache, you first need to enable ImageView to draw image cache.

then:

Bitmap bmap = imageView.getDrawingCache();

Also, calling buildDrawingCache(); is equivalent to calling buildDrawingCache(false);

Upvotes: 8

Related Questions