简体   繁体   中英

how to read images from “Drawable” instead of “SD Card” Android

i am beginner in android programing, and i created a Full Screen Image Slider app using this tutorial:

Link: http://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/

in this tutorial it reads images from Sd Card but i want to read images from drawable folder, i don't want to access images from SD Card instead access images from drawable.

This is the way I've seen, I hope it is correct:

private Bitmap bitmap = null;

private void createBitmap(Context context)
{
    Resources resources = context.getResources();
    Drawable d = resources.getDrawable(R.drawable.imagename);
    Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
    this.bitmap = bitmap;
}

It can be done like this:

 Drawable drawable = this.getResources().getDrawable(R.drawable.image);

where this refers to the context of the actual activity.

OR

you can just give the name of the image and then you can get the image using getIdentifier ,

getResources().getIdentifier(name,"drawable", getPackageName());

Where name will be the name of your image ie - "image1"

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