In my app i have an ImageView
. Before adding that ImageView
the app was performing smooth. now it throws ANR.
The image is saved in the database as base 64 encode string and it is decoded to bitmap and loaded to the imageview using :
imageView.setImageBitmap(bitmap);
The conversion of bitmap
and applying the bitmap
to ImageView
all those things are done in an AsyncTask
:
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private String data = "";
public BitmapWorkerTask(ImageView imageView, String data) {
imageViewReference = new WeakReference<ImageView>(imageView);
this.data = data;
}
@Override
protected Bitmap doInBackground(Integer... params) {
byte[] decodedString = Base64.decode(data, Base64.DEFAULT);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inPurgeable = true;
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length, options);
return decodedByte;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
The AsyncTask
is called from the main ui using the following code:
BitmapWorkerTask task = new BitmapWorkerTask(pollWebView,decodedStrings[1]);
task.execute();
decodedStrings[1] contains the base64 encoded image dataUrl.
Any solutions for this problem?
The major issue which is letting your app slow is WeakReference remove it and try whithout that
don't use this
private final WeakReference<ImageView> imageViewReference;
just use
private final ImageView imageViewReference;