繁体   English   中英

如何打开存储在Android中Assets下的文件夹中的图像

[英]How to open an image that is stored in a folder under Assets in android

我在Android Studio中的Build,Libs和Src的App下创建了一个Assets文件夹目录,在其中分别放置了带有图像的文件夹。 我有一个问题,找不到包含该文件名或路径的文件,但我知道它的正确性,请告诉我在遇到问题时该怎么做。 该文件位于此目录Assets / profileicon / 26.png中,该数字由profileIconTag(在本例中为26)确定,我是否为.open()设置了正确的路径名?

AssetManager assetManager = getAssets();
    InputStream istr = null;
    try {
        istr = assetManager.open("/profileicon/" + profileIconTag + ".png");
    } catch (IOException e) {
        e.printStackTrace();
    }
    Bitmap bitmap = BitmapFactory.decodeStream(istr);

    profileIcon.setImageBitmap(bitmap);

检查资产文件夹是否在正确的路径中。

InputStream bitmap=null;
try {
    bitmap=getAssets().open(profileIconTag +".png");
    Bitmap bit=BitmapFactory.decodeStream(bitmap);
    profileIcon.setImageBitmap(bit);
} catch (IOException e) {
    e.printStackTrace();
} finally {
   try{
    if(bitmap!=null)
    bitmap.close();
   }catch(Exception ex) {}
}

您可以使用AssetManageropen()方法获取InputStream ,然后使用BitmapFactory的decodeStream()方法获取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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM