简体   繁体   English

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

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

I have created an assets folder directory under App with Build, Libs, and Src in android studio, I have placed folders in it with images in each respectively. 我在Android Studio中的Build,Libs和Src的App下创建了一个Assets文件夹目录,在其中分别放置了带有图像的文件夹。 I have an issue that I cannot find the file in with that filename or path but I know its correct, please tell me what to do here as I am stumped. 我有一个问题,找不到包含该文件名或路径的文件,但我知道它的正确性,请告诉我在遇到问题时该怎么做。 The file is in this directory assets/profileicon/26.png and the number is determined by the profileIconTag (in this case its 26) now am I doing the right path name for the .open()? 该文件位于此目录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);

Check if the assets folder is in the correct path. 检查资产文件夹是否在正确的路径中。

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) {}
}

You can use AssetManager to get the InputStream using its open() method and then use decodeStream() method of BitmapFactory to get the Bitmap. 您可以使用AssetManageropen()方法获取InputStream ,然后使用BitmapFactory的decodeStream()方法获取Bitmap。 Check this link . 检查此链接

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