I am encoding a bitmap to base64.
after i pick the image from image picker, and set it to imageView, i convert it to base64. which i carry out on background thread. It gives me the same feel as if i am carrying out the operation on UI thread itself. i.e. my UI freezes for 10-20 secs until the encoding is complete.
what can be done? so that my UI doesnt freezes.
where am i going wrong?
My function to display the chosen image in imageView
private void showSelectedImage(Uri selectedImageUri) {
blurred.setImageBitmap(null);
imglocation = selectedImageUri.toString();
Ion.with(this).load(selectedImageUri.toString()).asBitmap().setCallback(new FutureCallback<Bitmap>() {
@Override
public void onCompleted(Exception e, Bitmap result) {
if (e == null) {
blurred.setImageBitmap(result);
blurred.buildDrawingCache();
bitmap = blurred.getDrawingCache();
new Convert2base64().execute(bitmap);
}
}
});
}
This is my AsyncTask
private class Convert2base64 extends AsyncTask<Bitmap, Integer, String> {
protected String doInBackground(Bitmap... urls) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image = stream.toByteArray();
String img_str = Base64.encodeToString(image, 0);
return img_str;
}
protected void onPostExecute(String result) {
bio.setText("Base64 encoded String\n" + result);
}
}
EDIT //blured is my imageView which is just a name rightnow. i am not carrying out the blurring task right now. I set the image picked by image picker in the Imageview, Thats fine, in the main thread, next two lines, i.e. buildDrawingCache() and getDrawingCache() cannot be called in background thread and is only to be run in the UI thread, I dont get any error in the process, it works as intended. But since i carry the task on the background thread, i am expecting it not to freeze the UI when its converting the image in the background.