How to Get the BitMap from a NetworkImageView - android

I have a NetworkImageView where I download images from a server. However, the requirement is to have the ability to rotate the image after downloading the image. I have a rotate image function but I need to get the Bitmap associated to the NetworkImageView in order for me to rotate the bitmap. How can I access the BitMap from a NetworkImageView?

You can use below method to get bitmap of any view.
Bitmap bitmap = viewToBitmap(mImageView);
Bitmap bitmap = viewToBitmap(mLinearlayout);
Bitmap bitmap = viewToBitmap(mRelativelayout);
public Bitmap viewToBitmap(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}

Hope this helps
UPDATE
Create a volley image request.
After the image has been loaded and set inside the NetworkImageView this code will work. Else it will return a Null pointer since there is no view/image inside the NetworkImageView
mNetworkImageView = (NetworkImageView) view.findViewById(R.id.networkImage);
Bitmap bitmap = ((BitmapDrawable) mNetworkImageView.getDrawable()).getBitmap();
There is another way to use Volley and obtain the Bitmap.
Create an ImageRequest
inside the onResponse(), obtain the Bitmap and set it to your ImageView (Not NetworkImageView). I had mentioned this method in the comments above.

Related

android - recyclerview - get bitmap

How can I get the bitmap from a recyclerview?
recyclerview.setDrawingCacheEnabled(true);
Bitmap bitmap = recyclerview.getDrawingCache();
recyclerview.setDrawingCacheEnabled(false);
 
Bitmap newBmp = bitmap.copy(bitmap.getConfig(),true);
And I'm getting an exception
java.lang.IllegalStateException: Can't copy a recycled bitmap
Change your code like this:
recyclerview.setDrawingCacheEnabled(true);
Bitmap bitmap = recyclerview.getDrawingCache();
Bitmap newBmp = bitmap.copy(bitmap.getConfig(),true);
recyclerview.setDrawingCacheEnabled(false);
Basically what's happening here is that when you call setDrawingCacheEnabled(false), the underlying Bitmap object that has been created is recycled. If you copy the bitmap before you recycle it, it should work as expected.

How to get bitmap from imageView

I need to take an image from imageView and make it as a bitmap.
String s = getIntent().getStringExtra("ImageUri");
mImageViewFilter = (ImageView) findViewById(R.id.imageViewFilter);
Glide.with(this)
.load(s)
.into(mImageViewFilter);
Bitmap bitmap = ((BitmapDrawable)mImageViewFilter.getDrawable()).getBitmap();
You can see that into mImageViewFilter I am loading a photo with use of its URI and Glide library.
My next step is to take this photo which is in mImageViewFilter and make it as a bitmap. After the last line of code there is an exception. What am I doing wrong?
String s = getIntent().getStringExtra("ImageUri");
mImageViewFilter = (ImageView) findViewById(R.id.imageViewFilter);
To load image into ImageView use below code
Glide.with(this).load(s).into(mImageViewFilter);
So in order to get bitmap from ImageView (mImageViewFilter )
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
OR
You can add this before the code where you are trying to get Bitmap out of the ImageView
mImageViewFilter.setDrawingCacheEnabled(true);
In order to get the Bitmap out of the ImageView using DrawingCache, you first need to enable ImageView to draw image cache.
Bitmap bitmap = mImageViewFilter.getDrawingCache();
Actually you doing right thing. But the problem is Image loading is Asynchronous call.
And you called getBitmap() in right next line which will be called sequentially.
And it will give you null anyway cause image is not loaded yet .
Solution
if you want to do it in next direct step .You can use direct Bitmap callback.
Glide.with(this)
.asBitmap()
.load(path)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
imageView.setImageBitmap(resource);
// Use resource as bitmap save it for later.
}
});
First take it as bitmap, then take it again to apply to our ImageView :
Bitmap bitmap = Glide.with(this) //taking as bitmap
.load(s)
.asBitmap()
.into(100, 100) //width and height
.get();
Glide.with(this) //apply to imageview
.load(s)
.into(mImageViewFilter);
The straightforward way to obtain a bitmap from any View including ImageView is to get the drawing cache from it.
mImageViewFilter.buildDrawingCache();
Bitmap bitmap = mImageViewFilter.getDrawingCache();
But the above method may give you a low quality image.
I think you should directly decode the bitmap from the Uri using BitmapFactory
InputStream in = context.getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(in);
in.close();

How do I save the viewport bitmap of an imageview? - Android

I only want to save the bitmap of the image being displayed/cropped in the ImageView.
When I call this, it returns the entire bitmap:
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
Use this to get the bitmap from ImageView
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();

Can a bitmap point to another bitmap?

I have a bitmap downloader class which decodes a stream into a Bitmap object.
Can I do something like
Bitmap bitmap;
bitmap = object;
when I do imageview.setImageBitmap(bitmap)
would be the same as imageview.setImageBitmap(object)
Also, is it posible to create multiple instances of bitmap? like:
for(i = 0; i < 10; i++) {
Bitmap bitmap = new Bitmap(); // how to do this?
new BitmapDownloaderAsynctask(bitmap).execute(url);
}
when I do imageview.setImageBitmap(bitmap) would be the same as imageview.setImageBitmap(object)
Yes, it would be the same (as long as object is another instance of Bitmap)
In order to create Bitmap manually, there is a bunch of static methods Bitmap.createBitmap() exist to create bitmaps (Bitmap class)
As an example, here is the easiest way to create a bitmap:
Bitmap bmp = Bitmap.createBitmap(100, 100, Config.ARGB_8888); //100*100 bitmap in ARGB color space
EDIT:
If you need to keep bitmap reference unchanged, you need to decode stream in a separate bitmap and then copy content of this bitmap into your original bitmapHolder. You can do that by drawing on the canvas:
AsyncTask code:
.....
Canvas canvas = new Canvas(bitmapHolder); //this bitmap was passed to AsyncTask
Bitmap tmpBitmap = Bitmap.decodeStream(...);
canvas.drawBitmap(tmpBitmap, 0, 0, null); //this will copy your decoded bitmap into your original bitmap which was passed into AsyncTask
.....

How to get the whole bitmap attached to an ImageView?

I tried to get Bitmap attached to an ImageView, using ImageView.getDrawingCache(); But I found that the returned Bitmap was not the same as I'd like to get from the ImageView. It was always smaller than the real image.
I had known that, the method getDrawingCache() should not have the view if it is bigger than the screen as the visible portion of the view is only drawn and the cache holds only what is drawn.
Could I get the whole bitmap attached to a ImageView?
If you just want the Bitmap from a ImageView the following code may work for you:-
Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
I think that's what you wanted.
If your drawble is not always an instanceof BitmapDrawable
Note: ImageView should be set before you do this.
Bitmap bitmap;
if (mImageView.getDrawable() instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
} else {
Drawable d = mImageView.getDrawable();
bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
d.draw(canvas);
}
Your bitmap is stored in bitmap.
Voila!
Easiest way is to set tag in ImageView.
imageView.setImageBitmap(bitmap);
imageView.setTag(bitmap);
To get Tag from it
imageView.getTag();

Categories

Resources