Opening a File from assets folder in android

Mina Samy picture Mina Samy · Dec 19, 2009 · Viewed 59.3k times · Source

I have a .gif file inside the assets folder like this assets/Files/android.gif. when I try to open the file it throws an exception at the second line

AssetManager mngr=getAssets();
InputStream is2=mngr.open("Files/android.gif");

so Is it that I'm trying to open an image file despite that the same code works if I try to open a text file ? what can be the problem here.

Answer

Tofeeq Ahmad picture Tofeeq Ahmad · Dec 14, 2011

These Lines are working perfectly--

InputStream assetInStream=null;

try {
    assetInStream=getAssets().open("icon.png");
    Bitmap bit=BitmapFactory.decodeStream(assetInStream);
    img.setImageBitmap(bit);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if(assetInStream!=null)
    assetInStream.close();
}

If your image is very big then you should scale your image before decoding it into Bitmap. See How to display large image efficiently