简体   繁体   中英

cant open image in android studio (file not found)

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

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

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?

Your code is ok. Check your assets folder path.

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

在此处输入图片说明

First create asset folder like this.

在此处输入图片说明

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

Android code "ic_launcher" replace with your image name

1.Create asset folder and

create pics folder(inside of your asset folder)

paste the image in pics folder

ImageView imageview;

2.Inside of Oncreate

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

3,Outside of 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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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