in my android application I've the same image in both the drawable folder and drawable-hdpi with different names (svs.jpg and svs1.jpg)
First, I've used svs.jpg (drawable folder)
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/svs"
/>
In one system it runs without any hindrance, but it shows error in another system (Inflate exception error in binary xml file). So I've used the image svs1.jpg as android:background="#drawable/svs1". Now it is working..
If I use svs1.jpg on the first system it shows error. I don't understand this problem. Can someone say why is this happening?
Images in different resource folders are meant to get the same name so the device will choose the one to use.
If they don't, I suppose some device are looking for the image name in the 'drawable' and crash if they don't find it, other device will look in the 'device-hdpi' folder and crash if they don't find it.
more information on supporting multiple screen here.
It seems you are really misunderstanding what these folders do.
Your android will select folder based on it's screen size or pixel-density of device on which u run it
If Your device has a medium Pixel density and a large screen. So it selects its resources from the res folders with those given qualifiers.
res/layout-large/my_layout.xml
and images from
res/drawable-mdpi/my_icon.png
You cannot tell your device to get images from the hdpi folder because it does not have a high pixel density.
So u give name of image same in all drawable folders .. Your device by default ll decide from which folder to take image , you need not worry about tat.
and just make sure the right images are in the right folders.
Related
I have kept different density images in 4 different folder. Example. In mipmap-hdpi, mdpi, xdpi, xxdpi, xxxdpi. And I'm using xiomi mi4i phone to test. By checking the density of phone I found out it's xxdpi.
I used ImagView to set image. Like
android:src="#mipmap/icon"
But what I'm finding out is it is taking from mdpi folder. I cleaned rebuilt project also. Nothing is working out. Can somebody please help what could be the problem.
For my verification what I did was I created new test project with only Imageview and kept four different size icons corresponding density folder. But still the image is picked from lower density folder.
You should use this line in xml :-
android:src="#drawable/icon"
I've a large image logo I'm currently scaling (usually down) to fill 50% of screen. At first I placed it in the base /res/drawable folder. It worked fine for most devices, but on some low-end phones with small screens the scaling introduced aliasing and it looks horrible. So I distributed the images as follows:
/res/drawable
-Large image, intended to be used on most devices
/res/drawable-normal
-Smaller image, intended to target smaller screens
However, when testing on Samsung Galaxy Tab 2 10' it picks the image in normal folder instead of the image in the base drawable folder. I know this tablet fits in the drawable-xlarge category since it is picking other resources from there. So I had to come up with this directory structure for the thing to work:
/res/drawable
-Large image, intended to be used on most devices
/res/drawable-normal
-Smaller image, intended to target smaller screens
/res/drawable-xlarge
-Same image as the drawable base folder, only used for the tablet to select the correct image.
Now it works as I want, but I can't understand why the system selects the normal folder for an xlarge device. Shouldn't it look first in the xlarge folder and then fallback on the base drawable folder?
Thanks in advance.
It falls back in a scaleable manner.
If it can't find Xlarge, then large is a closer match then normal is a closer match then the default folder
Reference:
How Android finds best matching resources
Android.Developer
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
I am on one really big project that was started sins the time of android 1.5.
This project evolved as android was evolving and at this point supports 2.3 and above.
Now I got this problem I have a lot of drawables in drawable-hdpi,drawable-mdpi,drawable-ldpi but also in drawable folder.
Generally the concept of this 3 folders(drawable-hdpi,drawable-mdpi,drawable-ldpi) it is clear to me but my confusion comes with the images in drawable folder.
for example I have an image named myimage.png and this image is present in drawable-hdpi,drawable-mdpi,drawable-ldpi folders with resolution 72x72,48x48,36x36 respectively but this image is also present in drawable folder.
My question is why ? when does the image from drawable folder is used ? is this image forgotten to be removed ? of it is used in some cases ? I have tried to remove this image and the application works just fine. I also have put just a green color with resolution 72x72 and this image is never drawn
so can someone clarify me the existence of the drawable folder ?
I know that if I do not have the dhpi,mdpi and ldpi folders then I need to put the images in drawable but what is the point of images in drawable when this 3 folders are present ?
Thanks
EDIT
one more question, what if I have image in drawable-hdpi but not in ldpi and not i mdpi. Let say we have phone with mdpi resolution density... What will happen ?
drawable folder is used for default images if the images are not found in the respective folders then it will look into the drawable folder.
I found this phrase in the doc:
For example, imagine you have an application icon, icon.png, and need unique version of it for different locales. However, two locales,
English-Canadian and French-Canadian, need to use the same version.
You might assume that you need to copy the same image into the
resource directory for both English-Canadian and French-Canadian, but
it's not true. Instead, you can save the image that's used for both as
icon_ca.png (any name other than icon.png) and put it in the default
res/drawable/ directory. Then create an icon.xml file in
res/drawable-en-rCA/ and res/drawable-fr-rCA/ that refers to the
icon_ca.png resource using the element. This allows you to
store just one version of the PNG file and two small XML files that
point to it. (An example XML file is shown below.)
Please refer this link for more clearity.
AFAIK you don't need drawable folder if you have all three types of folder (drawable-hdpi,drawable-mdpi,drawable-ldpi )with different resolution images.
As per I faced when you run the app on Android 1.5 and 2.0 if the images which is used in any layout is not available in folder 'drawable' then the project gives error "Resource not found" even if the image is present in other hdpi,mdpi or ldpi folder.
This is not about the densities there are other qualifiers as well, though in most of the cases we don't use them much. For list of qualifiers check here and here. You can use -finger -language ... etc qualifiers. In case you want to provide qualifier default resources[in this case drawable] you can opt for default drawable folder. Or many default folders without qualifiers added behind them
I think this page should help you understand better. Thank you for the question. I, myself, didn't know the answer. Basically you have 4 different versions and if no exactly matching version exists, the default one (without qualifier) is used.
I am new to Android and I need to use images in my XML file.
A tutorial says that I have to place them in drawable directory, but I can't find it as I find drawable-hdpi, etc.
drawable folder is divided into into three part according to device screen size h- high, M- Medium, L- Low because in android different size of device available in the market and android device screen divide into three type h,m,l based on density specific according to device size android pick the image from specific drawable folder h ,m ,l if you dont want to density specification in your application then add new folder with the name of drawable.
I hope it is more use full to you.
You can create the drawable folder yourself by right-clicking "res" -> "New" -> "Folder" and naming it drawable.If you do not need your images to be density-specific, you can put your images there.
you can create your own drawable folder in res directory. But remember keep the images in that folder which are common for all screen size devices. drwable-hdpi means this directory contained the images will be loaded when the device has higher dpi. similarly drawable-mdpi and drawable-ldpi are there.
Those which you found are drawable folders.. Insert the images in all three of them. So that at time of change in definition of screen images can be changed accordingly. For now, Insert same image in all three of them.
You can create your own drawable folder. But its good to use these at first then when you run your application on device you will come to know the difference.
drawable-hdpi drawable-mdpi etc are the different type of drawables . you can keep your images in these folder (any one at the initial level).
But they have some diffrence according to the resolution of the screen & density of android device. Further you can check the diffrence between them and keep the images as per need.
see this for more details: Explain the difference between drawable, drawable-ldpi, drawable-mdpi and drawable-hdpi
and Supporting Multiple Screens
you can create a drawable folder in the /res folder of your project and put your images there.
drawable-hdpi(mdpi/ldpi) are used separate different resources for different type of screen. take a look here to know more about multiple screen handling
I realize that this question is rather dated, but...it came up when I Googled the issue of inserting images into an Eclipse Android project, so....
Actually, those folders are mipmaps and they are used by the graphics subsystem to provide seamless zooming, as well. I would suggest creating proper mipmaps using an editor, as opposed to providing only one resolution choice.