Reputation: 147
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?
Upvotes: 1
Views: 2123
Reputation: 477
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;
}
Upvotes: 0
Reputation: 17131
First create asset folder like this.
Second create folder pics in asset and copy all into that folder files.
Upvotes: 1
Reputation: 302
Your code is ok. Check your assets folder path.
src->main->assets->pics->test.png
Upvotes: 1