Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a File from assets folder in 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.

like image 789
Mina Wissa Avatar asked Dec 19 '09 14:12

Mina Wissa


People also ask

How do I read an asset file?

If you cannot open your ASSETS file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a ASSETS file directly in the browser: Just drag the file onto this browser window and drop it.

What is the use of assets folder in Android?

Assets provide a way to include arbitrary files like text, xml, fonts, music, and video in your application. If you try to include these files as "resources", Android will process them into its resource system and you will not be able to get the raw data.

What method can be used to access a file in the asset's directory of your application?

Place your text file in the /assets directory under the Android project and use AssetManager class as follows to access it.

Where are Android assets stored?

I noticed that , when an android app is installed, It's apk file is copied to "data/app" directory. If I open it from there - just selecting view, I see assets folder which contains my files. That is correct. The APK is copied there during installation, but the asset files are left inside it.


1 Answers

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

like image 55
Tofeeq Ahmad Avatar answered Sep 21 '22 22:09

Tofeeq Ahmad