I need to change the source of an ImageView dynamically. I have a whole bunch of them with the same names, stored in their different dpi directories (drawable-hdpi etc) under res/. For this I've been using ImageView's setImageResource() and passing it a value from an array of resource IDs I've created at runtime like so:
decorations = new int[]{
R.drawable.bird1,
R.drawable.flower2,
R.drawable.bird3,
..etc};
Anyway, for some reason, the 6th image gets corrupted into an alternative image used for other things, not one in the resources list.
What on earth is going on?
Turns out I had a corrupt/incorrect image or the image was in the wrong directory or something. Still, strange that it decided to just display the next image in the directory rather than just crashing with an Exception or something.
Related
I am creating an app where I change the background image every time a new quote gets displayed.
I am using ImageView and those pictures are all jpg-files. They are stored in the mipmap folder under xxhdpi, because the images are too big for the drawable folder.
However when I am setting the new ImageView using:
background.setBackgroundResource(backgrounds.get(counter));
the new picture will be displayed, but it has a delay of half a second. Thats not really cool and I just want to set the new pictues smoothly.
I dont think its useful to post the whole code. The most important parts:
The references are stored in an ArrayList like this:
ArrayList<Integer> backgrounds;
Right after onCreate() I am filling the list like this:
backgrounds = new ArrayList<>();
backgrounds.add((Integer) R.mipmap.background1);
backgrounds.add((Integer) R.mipmap.background2);
backgrounds.add((Integer) R.mipmap.background3);
backgrounds.add((Integer) R.mipmap.background4);
The pictures have a size between 100KB and 7MB.
Can anyone tell me why the loading is slow and how to improve it? Also, is it right to put those in the xxhdpi folder under mipmap?
Thanks for every answer!
There are some valid comments under your question, but I'll add mine as an answer :)
Decreasing image sizes are crucial - using JPG instead of PNG and decreasing resolution will save you some time and device battery.
Try placing those images under the drawable-nodpi folder - this will save you from unneeded image scaling. I presume that you only have one instance of the background in your app.
There is not too much you can do to improve the background setting speed, especially when the images are already baked inside your APK.
It is working properly now. The images were way too big and I reduced the size of them from 2-3MB to ~25KB. They still look good and load properly.
I have an expandableListView in my activity and passing data from JSON that I get from server. Everything is working fine Except the image part.
I'm getting image path as string within JSON for different sizes (48x48, 72x72, 96x96 etc.) but unable to figure out how to assign image according to the screen density. I think I can Add these images into the corresponding drawable folder and android would do the rest. But on searching I did not find any example on how to add images in drawable folder dynamically.
If there is any way to add images dynamically into drawable folder please let me know OR suggest any other appropriate solution.
You can't add images dynamically into the drawable folders.
But you can get the device's screen density programmatically. You can use that value in your api and return appropriate image(s).
You must understand whats drawables in android by this link
and you must know that you can't put any images into them dynamically, icons and static images must be into drawables folders, for other images which you are using them into application you can get image and thumbnail
it's not possible to save in drawable at runtime.
you can't use assets folder too.
if I were you it I didn't bother my self to determine screen density. use Glide and Picasso in order to load image asynchronously.
Update :
the thing that i should mention is that, if the ImaveView is a fixed size view by dp it'll appear "the same size" on all devices as per Android's specs.
glide and Picasso read the view size by pixel and then scale the image to that size.
I prefer Glide because it has a smaller memory footprint by making some different assumptions like caching the resized images instead of the full-sized images.
Edit:
#greenapps you are completely right. it cannot be saved in assets too. try to create a directory and save drawable into that
I've had an app developed and have the responsibility of maintaining it, which means learning the Eclipse ADT environment. Nearly 20 years in web dev gives me some comfort, but this is certainly a new experience.
In one of the screens shown in the Graphical Layout window, a graphic source is indicated in the Properties panel as:
Src #drawable/ordo_search
ordo_search, obviously being the name of the PNG graphic, drawable appearing to be the folder.
But there are 4 folders holding graphics for this app, all beginning with the word drawable. They are:
drawable
drawable-hdpi
drawable-large-mdpi
drawable-sw600dp-hdpi
By altering this particular image and seeing the change come up in the Graphical Layout, I've determined that this graphic resides in the one called drawable-sw600dp-hdpi. In other areas of the app, I've determined in the same way that graphics are being pulled from any of the 4 folders, but in all cases the properties source paths all read the same: #drawable
Somewhere that #drawable attribute is being told an absolute path to where that graphic is, and that's what I need to find: where would I find and edit the path to that, or any, graphic?
Obviously I'm just getting to know the environment, so bear with me if you would.
It's not possible to get the path
This path will differ from device to device due to different dpi's of devices, it can point to any of the 4 folders you defined. If you want the drawable image you can get it via code by using getResources().getDrawable(R.drawable.yourdrawablename);
This will return your drawable and you can use it to display in a ImageView or where ever you want.
I don't know how to get the images what are available on the scree. I'm using imageview for the purpose of display the images on the screen.
Images will be changed randomly because i stored the images into array and images are in drawable. I want to know which image is displays on the screen at runtime.
thank u
When you change the image in the ImageView, hold onto the array index that you used in another variable.
How to pick image from particular folder, say sdcard/mypics?
i can recommend the gridview tutorial.
you will have to modify the code a bit to get the resource into an array before showing the gridview. but therefore you can use the BitmapFactory class.