如何在安卓中显示来自资产文件夹的图像?

14

我将我的图像存储在资产文件夹中,并尝试显示它们。我尝试了很多方法,但仍然无法显示图像。在logcat中没有显示任何错误。

请帮助我解决这个问题..........

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);
2个回答

29

你可以使用AssetManager来获取输入流,使用其open()方法,然后使用BitmapFactorydecodeStream()方法来获取位图。

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

0

这里似乎有些不对劲,你遇到了什么错误?因为你的问题中提到的错误不可能是由下面的代码引起的...如果有的话,它会说“无法打开内容:file:///testing/test3.png。”

听起来像是你的图片存储在错误的位置,这就是为什么它说找不到它们的原因。


我在资产文件夹中创建了图像并提供了该路径。您能告诉我如何设置该图像的路径吗? - hemanth kumar
如果它们在资产文件夹中,那么你做得很对,正如我所说,你上面的代码错误不匹配。你改过代码了吗? - Chris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接