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();
Related
I know this question was asked a lot before but every time I try to retrieve the Bitmap from my ImageView it return null object
I tried :
ImageView imageView = findViewById(R.id.image);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
And
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
And also with glide.
Am I missing something or is there another way to achieve this ?
Thanks for help
Try this, hope it helps
ImageView imageView = findViewById(R.id.image);
BitmapDrawable bd = (BitmapDrawable) imageView.getDrawable();
Bitmap b = bd.getBitmap();
I have a kotlin solution.
val bitmap = binding.myImageView.getDrawable().toBitmap()
I hope that it has been useful.
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.
I have used the code below to get bitmap, but I am getting only half of the image has been showing.
Bitmap bm = BitmapFactory.decodeResource(context.getResources(),R.drawable.ca_buyingpower);
I know how to crop a bitmap image from resources, but i want to cache image from ImageView to Bitmap, and then i want to crop it.
So, here is my code:
mImageViewArt.buildDrawingCache();
Bitmap bmap = mImageViewArt.getDrawingCache(); // works good
Bitmap bmp= BitmapFactory.decodeResource(getResources(),bmap); // Doesn't work(Cannot be aplied Int to Bitmap)
Bitmap resizedbitmap1 = Bitmap.createBitmap(bmp, 0, 0, 100, 100);
Why are you using the bmp intermediate variable?
If you want to crop the image returned by getDrawingCache(), you should just pass it as the input to Bitmap.createBitmap(), i.e.
mImageViewArt.buildDrawingCache();
Bitmap bmap = mImageViewArt.getDrawingCache();
Bitmap resizedbitmap1 = Bitmap.createBitmap(bmap, 0, 0, 100, 100);
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();