tags:

views:

515

answers:

3

Hi all I'm new to android 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. thanks

A: 

Don't know if things have changed or not but I had an app in Android 1.1 that opened icons to then display them in a view and I did it like so:

BufferedInputStream buf = new BufferedInputStream(mContext.openFileInput(value));
Bitmap bitmap = BitmapFactory.decodeStream(buf);
JRL
+1  A: 

I believe the preferred way to do this is to put your image in the res/drawable directory. Then you can get a Drawable like this:

Drawable d = Resources.getSystem().getDrawable(R.drawable.android);
AdamC
A: 

I suspect you are getting complaints about unhandled exception type IOException. If that's the case, you need to put the call to mgr.open in a try-catch block to handle the exception that may occur when retrieving the InputStream object.

AssetManager mngr = getAssets();
try {
    InputStream is2 = mngr.open("Files/android.gif");
} catch (final IOException e) {
    e.printStackTrace();
}
keno