The following line is throwing an OutOfMemoryException
if (null == myImage.getDrawable()){
//...
}
Is there another way to check if an ImageView has a Drawable/Bitmap?
This would be work around. Call imageview.getWidth() or getHeight(). If the value is 0 then no image is loaded
In this case, what i would do is make use of setTag and getTag on ImageView. First time in xml set the tag as empty and whenever you bind any drawable, set some value to the tag using setTag().
If tag has some value, assume that imageview is having a drawable and if the value is empty then the imageview is not having drawable.
Hope this helps you
You have to watch your memory usage though, here's a quick and dirty solution
try {
if (myImage.getDrawable() == null)
doFoo();
else
doBar();
} catch (OutOfMemoryError e) {
//the ImageView definetely had some large content
doBar();
}
You can overwrite ImageView class and fill some boolean value when setDrawable method called.
public class CustomImageView extends ImageView {
public boolean isImageSet = false;
public void setImageBitmap(Bitmap bm) {
super.setImageBitmap(bm);
if(bm != null)
isImageSet = true;
}
}
This might only address the symptoms, but you could try adding android:largeHeap="true" in your androidmanifest application tag
Related
I'm trying to show images from a URL in a ImageSwitcher. I've tried with:
myImageSwitcher.setImageURI(Uri.parse("http://miurl.com/images/foto01.jpg"));
but it said me this "error":
I/System.out﹕ resolveUri failed on bad bitmap uri: http://miurl.com/images/foto01.jpg
Thanks
I know it is late but because I found a better solution for this, would love to share it with all :)
I had to look at the source code of the default ImageSwitcher and know how it works. Whenever we use ImageSwitcher we set a ViewFactory to it. This ViewFactory creates Views which are then used in the ImageSwitcher. Usually we create an ImageView in the ViewFactory. This ImageView is then used in the ImageSwitcher class to draw the view with the image source specified. Below is the setImageResource() method of the ImageSwitcher class.
public void setImageResource(#DrawableRes int resid){
ImageView image = (ImageView)this.getNextView();
image.setImageResource(resid);
showNext();
}
Let's focus on the first and second line of the method.
ImageView image = (ImageView)this.getNextView();
image.setImageResource(resid);
this.getNextView() gets the view from the ViewFactory and casts the view into an ImageView. And then, the Drawable resource is set to it.
Solution
Now, with these information, let's get to the solution! I'm using Volley's NetworkImageView , but you can use Picasso or Glide too! You just have to change the code a bit.
We need to create a custom ImageSwitcher. Here's my code
MyImageSwitcher.java
public class MyImageSwitcher extends ImageSwitcher {
public MyImageSwitcher(Context context) {
super(context);
}
public MyImageSwitcher(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setImageUrl(String url) {
NetworkImageView image = (NetworkImageView) this.getNextView();
image.setImageUrl(url, AppController.getInstance().getImageLoader());
showNext();
}
}
Notice the setImageUrl() method. We cast the view we get from getNextView() to a NetworkImageView and set the Url to it. If you want to use Picasso, then just cast it into an ImageView and then use Picasso as it needs to be used.
Activity
MyImageSwitcher imageSwitcher = (MyImageSwitcher) findViewById(R.id.imageSwitcher);
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
public View makeView() {
NetworkImageView myView = new NetworkImageView(getApplicationContext());
myView.setScaleType(ImageView.ScaleType.CENTER_CROP);
myView.setLayoutParams(new ImageSwitcher.LayoutParams(imageSwitcher.getLayoutParams()));
return myView;
}
});
If you want to use Picasso, then just use an ImageView.
Layout
<xyz.farhanfarooqui.loadingimages.MyImageSwitcher
android:id="#+id/imageSwitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
AndroidManifest.xml
Don't forget to include android.permission.INTERNET to your AndroidManifest.xml
Done!
Whenever you want to change the image, do it this way,
imageSwitcher.setImageUrl("http://miurl.com/images/foto01.jpg");
I've solucionated this problem using SliderLayot (https://github.com/daimajia/AndroidImageSlider), but I'll try to do with ImageSwitcher too.
I was trying to achieve the same thing when I landed here. I know it's and old question, but since I don't see an answer, I'll tell you the solution that worked for me.
First of all, I downloaded the image/s that I wanted to show in the imageswitcher and return them as a Bitmap using this code:
InputStream in = new java.net.URL(url).openStream();
Bitmap bitmap = BitmapFactory.decodeStream(in);
Then I set the bitmap in the imageswitcher using imageSwitcher.setImageDrawable(new BitmapDrawable(getResources(), bitmap)).
To use java.net.URL(url).openStream(), you'll need to add <uses-permission android:name="android.permission.INTERNET"> to your AndroidManifest.xmlfile.
Have in mind that you could change the download code to make it better (checking for timeouts for example), but this is just a basic solutions to start from.
Another thing about what you were using, I think you can't use setImageURI() to set an internet resource in your ImageSwitcher. Acording to this post, this method is only for content URIs particular to the Android platform (Note that they are talking about an ImageView in that post, but I think the same goes to this case).
I've a requirement in which an ImageView's enabled state is changed.
I have 2 images: one for the enabled state & another for the disabled state.
Without using a selector, how can I change the Image src when the setEnabled(true/false) is changed?
That is:
imageView.setEnabled(true);
imageView - use image imgEnabled.png
imageView.setEnabled(false);
imageView - use image imgDisabled.png
Thank You
Try this
if(imageView.isEnabled())
{
imageView.setBackgroundResource(R.drawable.imgEnabled);
}
else
{
imageView.setBackgroundResource(R.drawable.imgDisabled);
}
You can do it by setting the image yourself using setImageResource.
But this may not be appropriate, since your view's state may be changed from your code anywhere.
imageView.setEnabled(true);
imageView.setImageResource(R.drawable.imgEnabled);
imageView.setEnabled(false);
imageView.setImageResource(R.drawable.imgDisabled);
You can also write your own class that extends ImageView
and override the following method
#Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if(isEnabled())
{
//set drawable for enabled state here
}
else
{
//set drawable for disabled state here
}
}
you can use this to change image resource-
imageView.setBackgroundResource(R.drawable.image);
I would like to store image ID's of the images in ImageView into database....then use these IDs as to change the images in ImageViews dynamically...
Pretty much simpler than you think it is.
The trick is to use Tag, so after you set image to the ImageView, set a tag with value of the Drawable id, afterward you can retrieve the id by getting tag.
public void setImageTag(ImageView imageView, int resId) {
imageView.setImage.... <--- set your image
imageView.setTag(int);
}
public int getImageTag(ImageView imageView) {
return (int)imageView.getTag();
}
I've implemented (thanks to a tutorial) a way to show a custom listView, with images, names and description.
I used the simpleAdapter to pass the strings name and description to the textViews, and the int of an image's Id (the image is stored in R.drawable) to the imageView.
The problem is: now i should set the ImageViews to display images downloaded from the web, so I have drawables but i can't get IDs from them.
How can i customize the simpleAdapter to pass a Drawable to the imageView, and set the image with the setImageDrawable method?
Please be clear! Thanks
We need to use the viewBinder, as show here:
adapter.setViewBinder(new ViewBinder() {
public boolean setViewValue(View view, Object data,
String textRepresentation) {
if(data instanceof BitmapDrawable ){
((ImageView)view).setImageDrawable((Drawable)data);
return true;
}else{
return false;
}
}
});
Try This:
http://www.androidpeople.com/android-load-image-from-url-example/
Or to load from External media files you have already downloaded:
Bitmap myBitmap = BitmapFactory.decodeFile(“/sdcard/myImage.jpg”);
ImageView myImage = (ImageView) findViewById(R.id.imageToShow);
myImage.setImageBitmap(myBitmap);
This is quite difficult... so I would advise to use CommonsWare's ThumbnailAdapter.
I have an ImageView with a source image set in the xml using the following syntax:
<ImageView
android:id="#+id/articleImg"
style="#style/articleImgSmall_2"
android:src="#drawable/default_m" />
Now I need to change this image programmatically. What I need to do is delete the old image and add a new one though. What I have done is this:
myImgView.setBackgroundResource(R.drawable.monkey);
It works but I noticed android stacks the new image on top of the old one (dont ask me how I found out it's not relevant for the discussion :). I definitely need to get rid of the old one before setting the new image.
How can I achieve that?
Changing ImageView source:
Using setBackgroundResource() method:
myImgView.setBackgroundResource(R.drawable.monkey);
you are putting that monkey in the background.
I suggest the use of setImageResource() method:
myImgView.setImageResource(R.drawable.monkey);
or with setImageDrawable() method:
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey));
*** With new android API 22 getResources().getDrawable() is now deprecated. This is an example how to use now:
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey, getApplicationContext().getTheme()));
and how to validate for old API versions:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey, getApplicationContext().getTheme()));
} else {
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey));
}
You're supposed to use setImageResource instead of setBackgroundResource.
myImgView.setImageResource(R.drawable.monkey);
is used for setting image in the current image view, but if want to delete this image
then you can use this code like:
((ImageView) v.findViewById(R.id.ImageView1)).setImageResource(0);
now this will delete the image from your image view, because it has set the resources value to zero.
get ID of ImageView as
ImageView imgFp = (ImageView) findViewById(R.id.imgFp);
then Use
imgFp.setImageResource(R.drawable.fpscan);
to set source image programatically instead from XML.
Supplemental visual answer
ImageView: setImageResource() (standard method, aspect ratio is kept)
View: setBackgroundResource() (image is stretched)
Both
My fuller answer is here.
Or try this one. For me it's working fine:
imageView.setImageDrawable(ContextCompat.getDrawable(this, image));
If you want to set in imageview an image that is inside the mipmap dirs you can do it like this:
myImageView.setImageDrawable(getResources().getDrawable(R.mipmap.my_picture)
Just write a method for changing imageview
public void setImage(final Context mContext, final ImageView imageView, int picture)
{
if (mContext != null && imageView != null)
{
try
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
imageView.setImageDrawable(mContext.getResources().getDrawable(picture, mContext.getApplicationContext().getTheme()));
} else
{
imageView.setImageDrawable(mContext.getResources().getDrawable(picture));
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}