How to get a bitmap from ImageView

I want to get Bitmapfrom ImageViewdownloaded using Glide, for example:

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

I tried the following:

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

and

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

And none of them have worked for me so far.

How can this be achieved?

+6
source share
4 answers

Oh, this is a simple mistake. You must add this before the code in which you are trying to get the Bitmap from ImageView:

imageView.setDrawingCacheEnabled(true);

To get Bitmapfrom an ImageView using DrawingCache, you first need an enableImageView to draw an image cache.

then

Bitmap bmap = imageView.getDrawingCache();

In addition, a call is buildDrawingCache();equivalent to a callbuildDrawingCache(false);

+6
source

, ( , , ShareButton/ShareContent), , , , :

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

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

is currently setDrawingCacheEnabled out of date , so another solution uses

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

Or to get Bitmapout 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 very good for handling bitmaps. Refer to the documents in Raster Image Processing

0
source
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
0
source

Source: https://habr.com/ru/post/1665381/


All Articles