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.
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.
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.
Place your text file in the /assets directory under the Android project and use AssetManager class as follows to access it.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With