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.
Related
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();
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();
I want to set a BitMap from resources but the name of the image came in a string.
How can i set it. My code now is this.
String Nombre= results.getString(0);
RelativeLayout oneLayout = (RelativeLayout)findViewById(R.id.relative1);
ImageView imageView = new ImageView(getApplicationContext());
Bitmap bMap = BitmapFactory.decodeFile("res/drawable/"+Nombre);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.Nombre);
imageView.setImageBitmap(bMap);
oneLayout.addView(imageView);
This line is not working i know:
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.Nombre);
I need a form to do it, any help is wellcome
If I understand your question, you have the name of the image and want to get the resid and load the image?
Assuming you have an image with the file name in Drawable:
myimage.jpg
Try this snippet:
String nameOfImage = "myimage";
int resId = context.getResources().getIdentifier(name, "drawable", context.getPackageName());
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), resId);
the problem as to why "Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.Nombre)" to not working is because the name "Nombre" has a capital letter in it,thats not allowed.Remove it,clean your project and then run it
I have this error when I run this code. It says java.lang.RuntimeException trying to use recycled bitmap. They say I should use Bitmap.recycle() but I'm having this error. Is my code correct?
Here is my code:
ImageView image = (ImageView) findViewById(R.id.imageView1);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
image.setImageBitmap(bMap);
bMap.recycle();
Check out this code:
if (null != bMap && !bMap .getBitmap().isRecycled())
{
bMap .getBitmap().recycle();
} else {
log("bitmap is already recycled");
}
I think it will help you.
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();