I had a scenario where I have 2 images with same name but of different sizes in hdpi,mdpi folders like below:
drawable-hdpi/a.png
drawable-mdpi/a.png
I tried to access the images in my code as below:
ImageView iv = (ImageView)findViewById(R.id.img);
int drawableID = this.getResources().getIdentifier("a", "drawable", getPackageName());
iv.setImageResource(drawableID);
So, is there a way in which I can change settings in emulator so that I can create 2 different emulators so that it takes images from different folders, because as of now I am able to see only image from mdpi folder.
Can any one help me in sorting out this issue?
Try http://www.genymotion.com/
It's faster than common emulator, and you can easy create devices with different dpi.
Related
I just got the new Nexus 7 which pulls images from the drawable-xhdpi folder because it is 1920x1200. The Nexus 10 also pulls from the drawable-xhdpi folder. The same image looks too big on the Nexus 7 as a result (too big by 150% to be exact). Is there any way to specify a resourse (drawable) folder to use for a specific device?
It sounds like I should not be trying to over-ride the default drawable buckets since there are so many screen sizes and it could hinder me when I attempt to support even more devices. So then what is the best way to handle this problem? The below images illustrates my problem exactly. The play button gets scaled down from the Nexus 10 to the new Nexus 7 while the bearded guy gets scaled up so that it remains as the same physical size on both devices. My images are currently doing what the bearded guy image is doing but I would like it to do what the play/connect buttons do which is scale down on the New Nexus 7.
(top image = New Nexus 7
bottom image = Nexus 10)
In order to keep using the drawable resource folders, I had to do the following to differentiate the Nexus 7 2013 and the Nexus 10 since they both would use the xhdpi folder by default:
For the Nexus 7 I ended up using: drawable-sw600dp-xhdpi
For the Nexus 10 I ended up using: drawable-sw720dp-xhdpi
I had 2 sets of assets scaled differently in each folder.
You can't set a drawable folder for a specific device, and it's also impractical, since there are too many devices out there (see here for example).
However, if you are unhappy with the way android choose the buckets of which file to use for each device configuration, you can use your own algorithm. there are 2 options to where to put the files in this case:
put the image files in res/drawable-nodpi (or res/raw) . for each image add a suffix/prefix to show which configuration it's used for.
put the image files in assets . this is a more flexible method, since you can even create sub folders and any file name you wish.
in both methods, you should also use your own image-sampling method , depending on your own rules.
You can use the following code to get the size and then scale the image to the required size based on the size of the display.
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenDensity = metrics.densityDpi;
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
I'm currently looking into getting my android app to work on Kindle Fire. I've got artwork for both MDPI and HDPI screens, but I noticed that when I load the app up on the Kindle, it displays the MDPI artwork and stretches some of my artwork that I'm filling parent with a little more than I want.
I was wondering if there's any way on Android to under certain circumstances (like if I'm on a Kindle), force it to load from the HDPI artwork, instead of defaulting to MDPI.
I do realize that I could just save my HDPI artwork in the MDPI folder with a slightly different name and do a check for every resource, but that's a lot of overhead, not to mention an increase in the size of my app, which I'd also like to avoid.
Thanks
Update: Still looking at this one. I guess what I'm really getting at, is there a way for an android device to chose HDPI artwork instead of MDPI, even though the MDPI artwork exists?
The Kindle Fire is 1024x600 with 160 dpi, right?
You can try new resourses with that resolution. And place them in the MDPI folder.
Add layout-large at /res directory and copy your layout file there.
That way, with the Kindle Fire, you use the layout at layout-large pointing to bigger resources in the MDPI folder.
And make sure you always use nine-patch drawables for resources.
Hope this helps you.
Ended up using a hack solution in the meantime, but I came across this:
I don't want Android to resize my bitmap Automatically
Pretty much just needed to move my hdpi images into the nodpi folder (in order to avoid the scaling issues) and changed the names slightly (I added a _hd to the name). After that I made an image loader that takes in the name of the image I want and returns _hd images if device is hdpi or if it's kindle fire:
id = ctx.getResources().getIdentifier(string + "_hd", "drawable", context.getPackageName());
Note: The docs do discourage the use of getIdentifier(), as it is less efficient than just using the resource address, but I took a look at the load times and loading 1000 images with getIdentifier takes .25 seconds, which is fine with me especially since I'm not loading anywhere close to that many images.
You can try new resources with that resolution and place them in the MDPI folder. Add layout-large at /res directory and copy your layout file there. That way, with the Kindle Fire, you use the layout at layout-large pointing to bigger resources in the MDPI folder.
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.