Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何打开存储在android中资产下文件夹中的图像_Android_Bitmap_Assets - Fatal编程技术网

如何打开存储在android中资产下文件夹中的图像

如何打开存储在android中资产下文件夹中的图像,android,bitmap,assets,Android,Bitmap,Assets,我在android studio中的应用程序下创建了一个资产文件夹目录,其中包含Build、Libs和Src,我在其中分别放置了带有图像的文件夹。我有一个问题,我无法找到该文件名或路径的文件,但我知道它是正确的,请告诉我在这里做什么,因为我被难住了。该文件位于这个目录assets/profileicon/26.png中,其编号由profileIconTag决定。在本例中,它是26。现在我正在为.open创建正确的路径名吗 AssetManager assetManager = getAssets

我在android studio中的应用程序下创建了一个资产文件夹目录,其中包含Build、Libs和Src,我在其中分别放置了带有图像的文件夹。我有一个问题,我无法找到该文件名或路径的文件,但我知道它是正确的,请告诉我在这里做什么,因为我被难住了。该文件位于这个目录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) {}
}
您可以使用AssetManager的open方法获取InputStream,然后使用BitmapFactory的decodeStream方法获取位图。检查


因此,我不需要包含图像的路径?您的代码中有一个错误,您需要尝试并捕获您的ifbitmap=nullbitmap.close;这对我不起作用。好吧,根据我在原始帖子中提供的信息,我应该如何将路径名放在.open中,正如我所说的,我的图像路径是assets/profileicon/image.pngre移动第一个斜杠。我猜你传递的路径是错误的,请尝试@greenapps所提到的
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;
}