简体   繁体   中英

how to use images present in drawable-hdpi,drawable-mdpi,drawable-ldpi of res folder?

I am able to keep the images of different resolutions inside drawable-hdpi,drawable-mdpi,drawable-ldpi of res folder but do not know how to access the images from assets/www folder . Kindly help.

you need to do any thing more to handle multi-resolution.just get the image by id and system will pic automatically as per current resolution.

http://developer.android.com/guide/practices/screens_support.html#range

You can get an image id by (no matter of dpi):

R.drawable.your_image

So, with id you can do everything that you need (use search).

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 ...

You don't need the AssetManager. You can do

BitmapFactory.decodeFile("file:///android_asset/www/bullet.jpg")

Though storing Images in the Assets is not the best way to go. You will not be able to take advantage of the Android resource management system. So unless you have a compelling reason to do this, I'd suggest that you take a look at using the res folder and the resources system.

Update: Explanation The BitmapFactory has a method to decode a file via the decodeFile method. That's point one. Android lets you access a file in the assets folder via the file:///android_asset/{path} path. In your case, the Image at /image/Malay/bullet.jpg is the assets folder can be accessed via the the file:///android_asset/www/bullet.jpg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM