Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the image from assets folder in android?

Tags:

android

I have kept my images in assets folder and tried to display them. I tried in many ways but still I am unable to display the image. In logcat it is not displaying any error.

Please help me regarding this..........

AssetManager mngr = mContext.getResources().getAssets();
        is = mngr.open("test3.png");
        bitmap = BitmapFactory.decodeStream(is);
        Uri uriSavedImage=Uri.fromFile(pictures_directory);

        ((ImageView) view.findViewById(R.id.imageView1)).setImageBitmap(bitmap);
like image 831
hemanth kumar Avatar asked Nov 16 '11 11:11

hemanth kumar


People also ask

How do I access asset files on Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 – Right click app >> New >> Folder >> Assets folder.

Do images go in assets folder?

As previously mentioned, CSS, JavaScript, images, fonts, and so on can all be considered assets.

How do I access an asset file?

To view asset files in Files home, select Libraries and then select Asset Library . To view and edit asset files in Setup, enter Asset Files in the Quick Find box, then select Asset Files.


1 Answers

You can use AssetManager to get the InputStream using its open() method and then use decodeStream() method of BitmapFactory to get the Bitmap.

private Bitmap getBitmapFromAsset(String strName)
    {
        AssetManager assetManager = getAssets();
        InputStream istr = null;
        try {
            istr = assetManager.open(strName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeStream(istr);
        return bitmap;
    }
like image 165
Lalit Poptani Avatar answered Oct 22 '22 12:10

Lalit Poptani