I am trying to load an image from the asset
folder and then set it to an ImageView
. I know it\'s much better if I use the R.id.*
for t
Here you are,
public Bitmap getBitmapFromAssets(String fileName) {
AssetManager assetManager = getAssets();
InputStream istr = assetManager.open(fileName);
Bitmap bitmap = BitmapFactory.decodeStream(istr);
istr.close();
return bitmap;
}
According to Android Developer Documentation loading with bitmap can degrade app performane.Here's a link! So doc suggest to use Glide library.
If you want to load image from assets folder then using Glide library help you alots easier.
just add dependencies to build.gradle (Module:app) from https://github.com/bumptech/glide
dependencies {
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}
sample example :
// For a simple view:
@Override public void onCreate(Bundle savedInstanceState) {
...
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Glide.with(this).load("file:///android_asset/img/fruit/cherries.jpg").into(imageView);
}
In case not worked by above method : Replace this object with view object from below code (only if you have Inflate method applied as below in your code).
LayoutInflater mInflater = LayoutInflater.from(mContext);
view = mInflater.inflate(R.layout.book,parent,false);