How to get bitmap from imageView - android

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

Related

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

How to load image from database into android ImageView using "setImageURI" function?

I have to set image in Image view. I used Glide library to set image in recycler view, now when I click to recycler view item I want to set it into another activity.
I just take image url through intent and store it as a string variable.
final String image = intent.getStringExtra("Image");
Then I tried to set image using the following line of code.
displayImage.setImageURI(Uri.parse(image));
Note: displayImage is my targeted imageview.
And I could get the correct url here.
Well when you have already set image once in RecyclerView why not use Bitmap from that ImageView. This will save network data and time.
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bs);
intent.putExtra("byteArray", bs.toByteArray());
startActivity(intent);
Then in target activity
if (getIntent().hasExtra("byteArray")) {
Bitmap bitmap =
BitmapFactory.decodeByteArray(getIntent().getByteArrayExtra("byteArray"), 0, getIntent().getByteArrayExtra("byteArray").length);
imageView.setImageBitmap(bitmap);
}
You can not display image from URL using setImageURI();
You need to pass url to your activity than display it using Glide
Use this
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.ic_placeholder);
requestOptions.error(R.drawable.ic_error);
Glide.with(this)
.load(image)
.apply(requestOptions)
.into(displayImage);
Instead of this
displayImage.setImageURI(Uri.parse(image));

How to Get the BitMap from a NetworkImageView

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.

How to get bitmap from NetworkImageView?

In my project I used (Volley + NetworkImageView) to download some images and texts and showing them in a list view.. till here, I don't have any problem.
Now, I want to get bitmaps form NetworkImageView and I tried many methods like the following, but non of them worked for me.
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Another method:
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
Non of them worked..
Any Help is appreciated,,
You cannot get the Bitmap reference as it is never saved in the ImageView.
however you can get it using :
((BitmapDrawable)this.getDrawable()).getBitmap();
beacuse when you set it with Volley u do this:
/**
* Sets a Bitmap as the content of this ImageView.
*
* #param bm The bitmap to set
*/
#android.view.RemotableViewMethod
public void setImageBitmap(Bitmap bm) {
// Hacky fix to force setImageDrawable to do a full setImageDrawable
// instead of doing an object reference comparison
mDrawable = null;
if (mRecycleableBitmapDrawable == null) {
mRecycleableBitmapDrawable = new ImageViewBitmapDrawable(
mContext.getResources(), bm);
} else {
mRecycleableBitmapDrawable.setBitmap(bm);
}
setImageDrawable(mRecycleableBitmapDrawable);
}
however if you set your default image or error image or any other image in any other way you may not get BitmapDrawable but NinePatchDrawable for example.
here is how to check:
Drawable dd = image.getDrawable();
if(BitmapDrawable.class.isAssignableFrom(dd.getClass())) {
//good one
Bitmap bb = ((BitmapDrawable)dd).getBitmap();
} else {
//cannot get that one
}

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