Suppose I have an image, named "image.jpg" inside res/raw/ folder.
Then I load it with :
InputStream imageIS = this.getResources().openRawResource(R.raw.image);
Bitmap myImage = BitmapFactory.decodeStream(imageIS);
Looks like they're not scaled according to screen densities (ldpi/mdpi/hdpi/etc..).
How can I scale those images according to different screen densities ?
(I know that there is an easy way doing so, by placing it into drawable folder, but for some reasons, I'd like to place my image in raw folder)
By placing it in the raw folder, you've told Android you want the image exactly as it is. If you really want the system to scale your image according to size and density of the device, you have to place it in one of the drawable folders under res. If you just like to use the raw folder, then you apparently just like to keep your images as they are.
Related
I have some images which I don't intend to be scaled, and I've read they are supposed to be in the drawable-nodpi folder. That folder didn't exist in my project yet.
I created it, and moved the images into that folder. Beside the file name it says (nodpi) in brackets. Does this mean the image will not scale when being loaded into an imageview? Does this also mean it takes less memory to load into an imageview, if the image is just center cropped?
Yes, you're correct. The images isn't scaled according the device density. Here the excerpt from documentation:
If you have some drawable resources that the system should never scale
(perhaps because you perform some adjustments to the image yourself at
runtime), you should place them in a directory with the nodpi
configuration qualifier. Resources with this qualifier are considered
density-agnostic and the system will not scale them.
And don't forget to use png file type instead of jpeg.
I am directly adding images in drawable folders and it is coming fine with ImageView tag android:src="#drawable/image_name" but I wonder what to do with the other drawable folders as shown below.
Do I need to add same image in all drawable folders or my way of adding images in drawable folders is wrong?
Please help me!
The image is same, but the size should be different for each folders.
If you add the image only in drawable folder, same image will be loaded for all screen resolutions. So for eg: if the image size is small, this may result in bad image quality.
You can find a similar question here
I use Android studio plugin Android Drawable Importer
All these drawable folders represent different android phone densities. The best folder to put your images is drawable-xxhdpi as android automatically downscale or upscale the images depending on your devices density and most phones these days are on xxhdpi density.
If you put a image in drawable folder only there are chances that image may get distorted(as it upscales) in xxxhdpi density.
If you are putting images in xxhdpi ,make sure you create your images acc. to xxhdpi resolution.
If you don't feel the image is looking right in some phones than check the density of that phone and put an image in that specific density folder acc. to it's size(reduced or increased).
Just put it in the drawable folder, no need to put it in other drawable-... folders because those folders and contents inside are created automatically by android studio.
So far, I always created one nine-patch image and place it inside /res/drawable-hdpi.
But I saw some large projects which have multiple nine-page images, scaled and placed inside multiple res/drawable-xxx directories. Is this the wrong approach? Nine-patch was supposed to stretch across all screens, regardless of its DPI.
Also, if I am right and only one nine-patch is to be used, what is its default location - drawable-hdpi, mdpi, or some other directory inside /res?
Well, it really depends on quality.
If your image is just a square border, it can be a 72 dpi low res image put into the drawable folder and it would be enough.
If your image has rounded corners or other fancy elements that have to be scaled properly, you could make a 480 dpi version and put it in the drawable-xxhdpi folder. This will scale down (don't even think of scaling up, because of stretching/pixellating) good enough in most cases.
If you want the best quality in scaling, then make a version for each dpi drawable folder.
If you go to http://developer.android.com/guide/topics/resources/drawable-resource.html, you can see it there (citation):
file location:
res/drawable/filename.9.png
The filename is used as the resource ID.
For my app, I need to have same images with 2 or 3 different sizes
Or I wonder if it would be possible to take the drawables from another drawable-folder (ie: the same images with different resolutions are available like that)
Else, I should copy the same image several times in each drawable folder (mdpi, hdpi, xhdpi)
Ex: I have drawable ima in hdpi folder and I need the same images smaller and greater which are available in mdpi and xhdpi folder => How can i get them or do I have to copy the images I need in the hdpi folder ?
Excuse me for my bad english
Thanks for helping me
try like this programmatically..
InputStream is=this.getResources().getAssets().open("drawable.png");
is=this.getResources().openRawResource(R.raw.myDrawable);
or
If you want to use the path try the following :
private void showImage() {
String uri = "drawable/icon";
// int imageResource = R.drawable.icon;
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
ImageView imageView = (ImageView) findViewById(R.id.myImageView);
Drawable image = getResources().getDrawable(imageResource);
imageView.setImageDrawable(image);
}
But it's recommended to use the R references :
int imageResource = R.drawable.icon;
Drawable image = getResources().getDrawable(imageResource);
Source
drawable-hdpi drawable-mdpi,drawable-ldpi are just different folders to make difference between images quality depending on the screen resolution. The idea is to put the same images in different folder with different resolutions ...
folders named mdpi, hdpi, xhdpi is for random devices , I mean there are for devices with random screens. There are 4 type of android devices considering from screen. small, normal , large , extralarge and that's why there are these folders , why you don't want to change your pictures names? If it's didn't help can you explain your question more recognizable? I don't sure I andertood question normally :)
Regards Hayk Nahapetyan
I'm not sure that I completely understand you but as for me you should logically organize your drawables and put them to assets folder not to resources. Then you can use them as you want.
Also I think there are no way to access resources from another screen.
Hope I understand you post right.
EDIT
API Level 15 introduced Resources.getDrawableForDensity() method. As far as I understood there is no way to launch it on older versions.
I have two drawable folders: drawable-mdpi and drawable-ldpi
I want to keep this structure (i.e. I don't' want to move my images to /assets), so that Android will automatically pick the appropriate artwork depending on the device density, however, on occasion I need to access the larger drawable version on the smaller device.
Is there a way to access the drawable-ldpi folder from code? I thought the following might be the answer, but it did not work:
Uri path = Uri.parse("android.resource://com.example.test/res/drawable-ldpi/icon");
imageview.setImageURI(path); //assume imageview is already initialized etc.
I get a java.io.FileNotFoundException (no such file or directory) warning (it doesn't crash, but it just doesn't load either).
Thanks so much for your help!
probably not much help but there is Resources.getDrawableForDensity() but this is for API 15 :-(
Generally, if you ever need to use the HDPI version, keep ONLY the HDPI version of the image, and the lower density phone will automatically use the HDPI drawable because it has no choice (i.e. a lower resolution image with that drawable name does not exist).
If you really need to switch between the hdpi and mdpi version I would suggest using a different filename and swapping programmatically, or showing/hiding XML elements if you prefer doing it that way... but that seems a little heavy-handed.
ImageView image = (ImageView)findViewById(R.id.imageView);
Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.abc);
image.setImageBitmap(bm);
Try reading THIS LINK also for more study over hdpi and mdpi concept