简体   繁体   中英

How to display the image from assets folder in 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);

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;
    }

Something isn't right here, what error are you getting? Because the error in you question wouldn't come up from that code below... if anything it would say "Unable to open content: file:///testing/test3.png."

It sound like your images are stored in the wrong place and that is why it's saying it can't find them.

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