-2

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.

Veer3383
  • 1,785
  • 6
  • 29
  • 49
  • As far as I can tell, your line `new Convert2base64().execute(bitmap)` is being executed in the Main thread, create a new thread, and execute the Asynctast inside it.....When posting a questio like this, try to be as specific as possible, post the target device, the manifest, Logcats, and anything that you consider might help find an issue. – Bonatti Dec 04 '15 at 10:13
  • 1
    @Bonatti AsyncTask does execute on a new thread, isn't that the point of it? – Kevin Dec 04 '15 at 10:15
  • If you look at the Android example http://developer.android.com/reference/android/os/AsyncTask.html. Your code seems to be correct maybe you block your main thread somewhere else – Jelle van Es Dec 04 '15 at 10:17
  • this two code lines blurred.buildDrawingCache();, bitmap = blurred.getDrawingCache(); are causing the freeze – Rajen Raiyarela Dec 04 '15 at 10:19
  • Read http://stackoverflow.com/questions/3046766/android-drawing-cache I would try to comment the three lines before new Convert2base64().execute(bitmap); to see what happens. It's possible that your ImageView is not getting cached properly. – Gordak Dec 04 '15 at 10:39
  • @Gordak i get null pointer, if i remove that, buildDrawingCache() – Veer3383 Dec 04 '15 at 10:47
  • well give the Bitmap "result" instead of "bitmap" – Gordak Dec 04 '15 at 11:18

3 Answers3

2

Remove the setText from postExecute method, if you are checking the result then rather check in logcat using Log.d("response",result);

The encoded text is a large amount of data, as Marcin correctly pointed out. That large amount you are loading back in the UI thread by calling setText. Thats what is freezing the UI and not the encoding process.

Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
0

I don't know what the blurred view is, but I assume it's an ImageView that renders the image set to it as blurred. That blurring process is on your UI Thread, and thus causes the freeze.

Kevin
  • 1,626
  • 16
  • 30
  • yes, blurred is just an imageView. i missed to mention that, i have updated my question being little more specific – Veer3383 Dec 04 '15 at 10:31
0

Your code looks correct, you should profile it to find what takes so long. Ie. by using System.currentTimeMillis(). Actually, what looks strange to me is that you return this image as a string from doInBackground, this will be a very long string - and then you put it into a TextView. Maybe this is what takes so long. So first step is to comment out this setText line in onPostExecute and see if that helps.

marcinj
  • 48,511
  • 9
  • 79
  • 100