在android studio中无法打开图像(找不到文件)

时间:2022-07-26 07:28:30

I'm using android studio and I have some pics located here: ..\app\src\main\res\pics

我正在使用android studio,我在这里有一些照片:.. \ app \ src \ main \ res \ pics

I have this method to open images in the adapter java class

我有这个方法在适配器java类中打开图像

private Drawable drawImage(String iconString) {
    Log.i(TAG , "icon string = " + iconString) ;
    AssetManager assetManager=ctx.getAssets();
    InputStream inputStream;

    try {
        inputStream=assetManager.open("pics/"+iconString+".png");
        Log.i(TAG , "icon string try = " + iconString) ;
        Drawable drawable=Drawable.createFromStream(inputStream, null);
        return  drawable ;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return  null;

}

I did invoke the method but when I build and run it I get the following error:

我确实调用了该方法,但是当我构建并运行它时,我收到以下错误:

java.io.FileNotFoundException: pics/ic_payments.png
06-08 10:10:44.577 8002-8002/ ... W/System.err:    
at android.content.res.AssetManager.openAsset(Native Method)

What did I miss here?

我在这里想念的是什么?

3 个解决方案

#1


1  

Your code is ok. Check your assets folder path.

你的代码没问题。检查资产文件夹路径。

src->main->assets->pics->test.png

在android studio中无法打开图像(找不到文件)

#2


1  

First create asset folder like this.

首先创建这样的资产文件夹。

在android studio中无法打开图像(找不到文件)

Second create folder pics in asset and copy all into that folder files.

第二步在资产中创建文件夹pics并将其全部复制到该文件夹​​文件中。

#3


0  

Android code "ic_launcher" replace with your image name

Android代码“ic_launcher”将替换为您的图片名称

1.Create asset folder and

1.创建资产文件夹和

create pics folder(inside of your asset folder)

创建pics文件夹(在资产文件夹内)

paste the image in pics folder

将图像粘贴到pics文件夹中

ImageView imageview;

2.Inside of Oncreate

2.在Oncreate的内部

 imageview=(ImageView)findViewById(R.id.imageview);
 imageview.setImageDrawable(drawImage("ic_launcher"));

3,Outside of Oncreate

3,Oncreate之外

private Drawable drawImage(String iconString) {
    Log.i(TAG , "icon string = " + iconString) ;
    AssetManager assetManager=getAssets();
    InputStream inputStream;

    try {
        inputStream=assetManager.open("pics/"+iconString+".png");
        Log.i(TAG , "icon string try = " + iconString) ;
        Drawable drawable=Drawable.createFromStream(inputStream, null);
        return  drawable ;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return  null;

}

#1


1  

Your code is ok. Check your assets folder path.

你的代码没问题。检查资产文件夹路径。

src->main->assets->pics->test.png

在android studio中无法打开图像(找不到文件)

#2


1  

First create asset folder like this.

首先创建这样的资产文件夹。

在android studio中无法打开图像(找不到文件)

Second create folder pics in asset and copy all into that folder files.

第二步在资产中创建文件夹pics并将其全部复制到该文件夹​​文件中。

#3


0  

Android code "ic_launcher" replace with your image name

Android代码“ic_launcher”将替换为您的图片名称

1.Create asset folder and

1.创建资产文件夹和

create pics folder(inside of your asset folder)

创建pics文件夹(在资产文件夹内)

paste the image in pics folder

将图像粘贴到pics文件夹中

ImageView imageview;

2.Inside of Oncreate

2.在Oncreate的内部

 imageview=(ImageView)findViewById(R.id.imageview);
 imageview.setImageDrawable(drawImage("ic_launcher"));

3,Outside of Oncreate

3,Oncreate之外

private Drawable drawImage(String iconString) {
    Log.i(TAG , "icon string = " + iconString) ;
    AssetManager assetManager=getAssets();
    InputStream inputStream;

    try {
        inputStream=assetManager.open("pics/"+iconString+".png");
        Log.i(TAG , "icon string try = " + iconString) ;
        Drawable drawable=Drawable.createFromStream(inputStream, null);
        return  drawable ;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return  null;

}