Calling setImageBitmap not always work for ImageView - android

In my application the user selects a bitmap and then i use the following code:
BitmapFactory.Options op= new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(path,op);
imageView.setImageBitmap(bitmap);
Unfortunately, sometimes ImageView does not display anything
What can i do?

Sometimes bitmap is too large. Thats why imageview turns blank! I think that here you can find what you need: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

I found this problem for Android 8 and low. There is no difference use method setImageBitmap or setImageUri : ImageView will be blank sometimes. There is no any errors and you can get Bitmap from ImageView.
Solution.
Use width and height of Bitmap for ImageView no more then double screen size, better is to fit to the screen size.

Related

Refresh image cache in ImageView

I have an imageView from which I take visible part of image with getDrawingCache(). It works well. But when I change image in imageView and try to get bitmap from it, getDrawingCache() returns bitmap of the first image. I tried to call this method buildDrawingCache() before calling getDrawingCache() but it didn't help.
How can I refresh cache or associated bitmap or I don`t know what in order to get new image?
Solution was very simple:
imageView.destroyDrawingCache();
imageView.buildDrawingCache();
imageView.getDrawingCache();

Image in ImageView auto scale when display the same BitmapDrawable with another ImageView?

This situation I have met in Android and still can't find any solution.
I have an ImageView display by these code:
BitmapDrawable value = ...;
imageView1.setImageDrawable(value);
Then I use imageView2 to display the same image in Drawable:
imageView2.setImageDrawable(value);
And ops,... the image in imageView1 auto scale and become bigger than the older. (imageView2 is bigger than imageView1).
Is there anyone saw this situation before?
Drawables are immutable, so the one bitmap is getting scaled and then shown by both ImageViews.
To get a different (unshared) version for the second one, try:
imageView2.setImageDrawable(value.mutate());

Android avoid image scalling in Imageview for Image Mapping like Html

Hi Am trying to implement image mapping as like html in android. For that i referred this project. Its work good when i access the image from drawable-nodpi folder. But when i access from other drawable folder the image get scaled as per the android concepts. Here i used drawable folder for checking purpose only. Actually i get image from back end service.
I also tried with bitmap by setting the no density.
Bitmap bm;
bm=BitmapFactory.decodeResource(getResources(), R.drawable.blueprint);
bm.setDensity(Bitmap.DENSITY_NONE);
mImageMap.setImageBitmap(bm); //mImageMap is customized view which extends Imageview
But it's not working. Am also tried as per this post. Its not working.Is there any way to load image to image view without scaling?
you can use Bitmap.createScaledBitmap with the orignal mesaures of the picture..
int width = orignal bitmap width
int height = orignal bitmap height
Bitmap bm;
bm = getBitmap... from network
bm = Bitmap.createScaledBitmap(bm,width,height,true);
this will create a image scaled properly and just place it in the ImageView.

Images scales defferently in setImageDrawable() and setImageBitmap()?

I am new in android development. I have an issue regarding the image scaling in my app. When i set images using setImageDrawable() images look proper like in screenshot:
But if i set image using setImageBitmap() method it does not look proper. Some black color occurs around it like in screenshot bellow:
I have tried it in multiple applications and i observed that in some applications the image size is becoming smaller than the size ofter setting image through setImageDrawable() or setImageResource().
the reason for setting images through setImageBitmap() is that i want to set images to the imageviews which are kept in assets folder and not in drawable.
Following is the code i used to create a bitmap and set it to imageview:
BitmapFactory.Options opts = new Options();
opts.inPurgeable = true;
try
{
Bitmap preview_bitmap = BitmapFactory.decodeStream(assetManager.open(drawableFolder+"/"+tmpArr[0]),null,opts);
view.setImageBitmap(preview_bitmap);
}
catch (Exception e)
{
LNLog.writeTOErrorToLog(e.getMessage());
}
So, please can anyone tell me where i'm i going wrong or missing something to set.
I tried to set Options but did not worked. I want to set images as in first screenshot that is how they looks by using setImageDrawable().
Thanx in advance.
Note: in screenshots images are referent in text on it only.IF i set hindi images i.e. the images on which text is in hindi language using setImageDrawable() then they looks proper as in like in screen shot one.So , no issue of image files.
You can get rid of the black borders by using
android:adjustViewBounds="true" in xml
or
imageView.setAdjustViewBounds(true) in Java.
EDIT:
Then try using drawable only
Drawable d = Drawable.createFromStream(assetManager.open(filePath), null);
view.setImageDrawable(d)
This worked for me :
try
{
view.setAdjustViewBounds(true);
int height = view.getDrawable().getIntrinsicHeight();
int width = view.getDrawable().getIntrinsicWidth();
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPurgeable = true;
opts.inScaled = true;
Bitmap preview_bitmap = BitmapFactory.decodeStream(assetManager.open("drawable-mdpi/FAQ_hi_in.png",null,opts);
Bitmap bmp = Bitmap.createScaledBitmap(preview_bitmap, width, height, true);
view.setImageBitmap(bmp);
}
In this code I have taken the height and width of actual image and then scaled my bitmap to that height and width. Then I converted this scaled bitmap to drawable and set it into my ImageView, thats it. It resolved my problem of black border around image.

How to destroy a drawable inside an ImageView if we don't need it?

This question is related with Do we have to explicitly recycle the bitmap if we don't need it?.
There is an ImageView have a drawable, when user clicks a button, it will assign a new drawable to the ImageView.
Do we have to destroy the old drawable belongs to the ImageView, and how?
Drawable oriDrawable = imageView.getDrawable()
// set callback to null
oriDrawable.setCallback(null);
// get the bitmap and recycle it
((BitmapDrawable)oriDrawable).getBitmap().recycle();
Is the code above correct? What's the best solution?
You could try using something like:
Drawable drawable = imageView.getDrawable();
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
bitmap.recycle();
}
Where imageView is your ImageView.
Original answer here.
Is this just a general question or are you running out of memory? I would not make this optimisation until you really have a problem.
In general if you are loading bitmaps from drawable folders that are not large (large as in megabytes) then you should not really run into a problem.
First you need to make sure the assets you are loading are optimal for where you display them, for example there is no point in setting an ImageView to an image thats 1024 x 1024 in size if the area you display the image is a size of 64x64.
Breaking the bitmap budget is usually caused by loading in images of an unknown size or just simply getting the image sizes wrong as described above, swapping an ImageView frequently will generally not give you an issue with optimal sized images.
There is a great article in Android Training that covers loading bitmaps optimally http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Hope that helps

Categories

Resources