如何在Android中显示资产文件夹中的图像?

问题描述:

我已将我的图片保存在资产文件夹中,并尝试显示它们。我尝试了很多方法,但仍然无法显示图像。在logcat中它不显示任何错误。如何在Android中显示资产文件夹中的图像?

请帮我对此..........

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

您可以使用AssetManager使用其open()方法获取InputStream和再使用的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; 
    } 

东西是不正确这里,你得到什么错误?因为你的问题不会出现在下面的代码中......如果有的话会说“无法打开内容:file:///testing/test3.png”。

听起来像你的图像存储在错误的地方,这就是为什么它说它找不到它们。

+0

我在资产文件夹中创建了图像并给出了该路径。你能告诉我如何设置该图像的路径吗? –

+0

如果他们在资产文件夹中,那么你做的是正确的,因为我说你从上面的代码错误不匹配。你改变了代码吗? – Chris

+0

嗨,请检查一次代码。我编辑过它。 –