I looked at the 2 examples on Stack, but can\'t get them to work. I\'m simply trying to grab an image from a folder in assets and set it as in ImageView, but get a null pointer
Where is "file:///android_asset/Samples/" + imageName
coming from? If your hierarchy looks like assets/file_name.jpg, you would just call open(file_name.jpg)
. In other words, try replacing your file:///android_asset/Samples/" + imageName
with just imageName
.
Check out the API Demos, specifically the ReadAsset.java class:
try {
InputStream is = getAssets().open("read_asset.txt");
...
where the assets folder looks like
Frank I had the same problem !
I had my PNG images within "assets" of my project and AssetManager.open() kept on giving me an exception because it couldn't find the file !
I investigated by using assetManager.list("") to list what's in "assets". I subsequently discovered that my images were actually NOT added to the "assets" !
As you can imagine I was getting pretty pissed off at this point because obviously my images should have been within assets because I could see them play as day in Eclipse within the damn "assets" folder of my project.
Solution
Bonus - I updated your getBitmapFromAsset() method:
private Bitmap getBitmapFromAsset(String strName) throws IOException
{
AssetManager assetManager = getAssets();
InputStream istr = assetManager.open(strName);
Bitmap bitmap = BitmapFactory.decodeStream(istr);
return bitmap;
}