简体   繁体   中英

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" .

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

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