mukeshsoni
mukeshsoni

Reputation: 583

Get bitmap from imageview in viewpager with glide

I'm showing in imageview with glide. User can save image on button click.

Bitmap bitmap = ((BitmapDrawable)imageViewPreview.getDrawable()).getBitmap();

when i use this code it save whole screen image(Image and black space).

    imageViewPreview.setDrawingCacheEnabled(true);
    imageViewPreview.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    imageViewPreview.layout(0, 0,
            imageViewPreview.getMeasuredWidth(), imageViewPreview.getMeasuredHeight());
    imageViewPreview.buildDrawingCache(true);
    Bitmap bitmap = Bitmap.createBitmap(imageViewPreview.getDrawingCache());
    imageViewPreview.setDrawingCacheEnabled(false);

when is use this, sometime it work but image get cropped in half or image go to up like "android:layout_alignParentTop="true".

Upvotes: 0

Views: 1639

Answers (1)

SpiritCrusher
SpiritCrusher

Reputation: 21053

You can get Bitmap from DrawingCache. After Settting image in ImageView you can get it as below:

    imageViewPreview.setDrawingCacheEnabled(true);
    imageViewPreview.buildDrawingCache(true);
    final Bitmap bmp = Bitmap.createBitmap(layout.getDrawingCache());
    imageViewPreview.setDrawingCacheEnabled(false);

If you need full size image then this is not right way i think .A better solution would be to get it From Glide as Bitmap . If full image was loaded already it will be quick (from cache) if not then it will load the full image from server or you can still use Drawing cache in case of No internet connection and glide failure.

Glide.with(imageView.getContext())
    .asBitmap()
    .load("imageUrl")
    .into(new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
           // Use resource here    
           }
        });
    }

Upvotes: 2

Related Questions