How to load images for different densities from server - android

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

Related

Android drawable memory management

I implemented a RecyclerView in one of my Activities.
I use a customized RecyclerView adapter to load data from a SQLite Database (using Room and LiveData-structure) and display it in the view.
All of the rows have a large ImageView where I want to show a Drawable (just a .png). To do that I first tried to use the approach written on the official Android Developers site but it did not work well for me.
Therefore I am trying to use Glide as a library recommended by Google.
I have 16 drawables in my project. They all have a scaling between 800x800 and 1920x1080 pixels.
In my adapter I load the data from my database and based on the drawable id, the image as well (so there is no image data stored in the database; I did this previously).
Unfortunately my App cannot handle that amount of image cache which leads to an OutOfMemoryError exception. That's why I used
android:largeHeap = "true"
in my Android Manifest.
I know this is not a good solution and I also know that there has to be a way to use less memory for such a small amount of pictures.
In Glide I use
GlideApp
.load(R.drawable.my_drawable)
.fitcenter()
.into(myImageView);
But unfortunately the image does not get shrinked or smaller. I thought that Glide can scale the image based on the size of the ImageView, screen size and so on.
What am I doing wrong? Is there a better way to use less memory without fixed scaling so that my pictures not become ugly?
EDIT:
I first stored my images in the basic "drawable" folder in my project structure (copy and paste).
My second approach was the gimp-android-xdpi addon which exports images or icons for any android density (mdpi, hdpi, xhdpi etc.). But this did not help either.
call override(horizontalSize, verticalSize).
This will resize the image before displaying it in the ImageView.

How to prevent a drawable from becoming blurry?

When i copy-paste an image to the drawable file, i get one image with the original resolution. When i use the New--ImageAset way i get a lot of images for different screen densities but they are all blurry, even xxhdpi! How can i have the different densities inages without this problem?
Im surprise i didnt noticed it before!
I read this but it hasnt helped me
Android ImageView blurry?
http://developer.android.com/guide/practices/screens_support.html
Any advice am i doing it wrong?
EDIT
I tried the AndroidAssetStudio but it still happens
Before
After (xxhdpi)
Cant i get it to be the ldpi and then make it scale up from it or something?
You can create your image in different resolutions using Android Assets Studio. Then, copy paste the image to the respective folder. This will give you a proper image, without blur.
There is a plugin exactly made for the same purpose. You can use that plugin to make different sizes of the same image. You have to copy/paste a single image in drawable folder. Then right click drawable folder and select
NEW>Batch_Drawable_Import. Select the size for your image and it will itself make the other sized images for you.

same image, different sizes: with picasso

I have an image that is to be displayed in about four different sizes depending on which activity the user is viewing. For instance a ListView will show one size, a GridView will show one size, a slide show will show one size, etc. If I use Picasso, will it download the image once or will it download one image for each size? Of course, I am taking into account that Picasso caches images (which is what I want). The key point here is that I have a single url for the image since it is one image.
Note that to keep the example simple, I mention one image. But of course I am talking about a set of images each of which needs to be manipulated as mentioned in the paragraph above.
If I use Picasso, will it download the image once or will it download
one image for each size?
Once for the original size as you get from the URL.
You can use the resize() method to resize the image and the original image would still stay at full resolution. I have done that in my app where I displayed a 600x600 image at 150x150 in a thumbnail and in full resolution later.

What is the effective way to store over 200 emotion icons in Android application

I have over 200 emotion icons in my application.
And I put them in res/drawable.
And it really make the drawable folder so large although each icon just 1-3K.
Is there any other good methods to store these icons instead of storing them in drawable folder?
Also, I will display those icons in grid view to let users to select.
Is it good to store all in drawable folder and put them in the grid view?
You can merge all your images in single bitmap. If you keep the size fixed, then you can use Bitmap.createBitmap() in a loop to generate a bitmap array.
Follow this tutorial on splitting bitmap.
SPLITTING/DIVIDING AN IMAGE INTO SMALLER CHUNKS/PIECES IN ANDROID
How to Split a Bitmap into Pieces in Android

Different dimensions when using image from drawable and loading from sdcard

I have an application in which i am setting the background of an image view.
If i keep my images in the re/drawable(s) folder, everything works fine but if i keep the same images in the sdcard and load from there to set the background then the dimensions of the image are different.
A similar question had been asked earlier but it is still unanswered. Could anyone shed some light on this topic.
if we place the images in res/drawable(s) folder then android chooses the appropriate image according to target device and scaling is also done by system if required. So if you are putting a image in sdcard then system does not do any scaling on image.

Categories

Resources