0

I am trying to convert an image from a remote source (such as http://codeboxr.com/wp-content/uploads/2012/06/6_logo_predesign.jpg) and then convert it into a (byte array and then into) corresponding String. I then have to pass this String to another method which converts the String into the appropriate object and set it as the source for an ImageView.

Here is what I have tried so far.

URL file = new URL(imageURL);
URLConnection uc = file.openConnection();
InputStream imageInFile = file.openStream();
byte imageData[] = new byte[(int) uc.getContentLength()];
imageInFile.read(imageData);

// Converting Image byte array into Base64 String
String imageDataString = Base64.encodeToString(imageData, 0);
//Image successfully converted to string, ready to pass as a parameter!

Now converting imageDataString to be set as image for ImageView

    byte[] imageByteArray = receivedString.getBytes();
ImageView background = (ImageView)findViewById(R.id.imageView_background);
                background.setImageBitmap(BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length));

But this is not working. I am continuously getting errors on Runtime. Here is the stack trace.

12-26 20:34:03.288  11325-11325/samarth.learning.http D/dalvikvm﹕ GC_FOR_ALLOC freed 3203K, 63% free 4103K/10920K, paused 27ms, total 27ms
12-26 20:34:03.288  11325-11325/samarth.learning.http I/dalvikvm-heap﹕ Forcing collection of SoftReferences for 185326656-byte allocation
12-26 20:34:03.308  11325-11325/samarth.learning.http D/dalvikvm﹕ GC_BEFORE_OOM freed 70K, 64% free 4032K/10920K, paused 19ms, total 19ms
12-26 20:34:03.308  11325-11325/samarth.learning.http E/dalvikvm-heap﹕ Out of memory on a 185326656-byte allocation.
12-26 20:34:03.308  11325-11325/samarth.learning.http I/dalvikvm﹕ "main" prio=5 tid=1 RUNNABLE
12-26 20:34:03.308  11325-11325/samarth.learning.http I/dalvikvm﹕ | group="main" sCount=0 dsCount=0 obj=0x415d8728 self=0x415c2658
12-26 20:34:03.308  11325-11325/samarth.learning.http I/dalvikvm﹕ | sysTid=11325 nice=0 sched=0/0 cgrp=apps handle=1074503676
12-26 20:34:03.308  11325-11325/samarth.learning.http I/dalvikvm﹕ | state=R schedstat=( 0 0 0 ) utm=34 stm=26 core=0
12-26 20:34:03.308  11325-11325/samarth.learning.http I/dalvikvm﹕ at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
12-26 20:34:03.308  11325-11325/samarth.learning.http I/dalvikvm﹕ at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:429)
12-26 20:34:03.308  11325-11325/samarth.learning.http I/dalvikvm﹕ at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:447)
12-26 20:34:03.308  11325-11325/samarth.learning.http I/dalvikvm﹕ at samarth.learning.http.MainActivity$MyAsyncTask.onPostExecute(MainActivity.java:496)
12-26 20:34:03.308  11325-11325/samarth.learning.http I/dalvikvm﹕ at samarth.learning.http.MainActivity$MyAsyncTask.onPostExecute(MainActivity.java:330)
12-26 20:34:03.318  11325-11325/samarth.learning.http I/dalvikvm﹕ at android.os.AsyncTask.finish(AsyncTask.java:631)
12-26 20:34:03.318  11325-11325/samarth.learning.http I/dalvikvm﹕ at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-26 20:34:03.318  11325-11325/samarth.learning.http I/dalvikvm﹕ at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-26 20:34:03.318  11325-11325/samarth.learning.http I/dalvikvm﹕ at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 20:34:03.318  11325-11325/samarth.learning.http I/dalvikvm﹕ at android.os.Looper.loop(Looper.java:213)
12-26 20:34:03.318  11325-11325/samarth.learning.http I/dalvikvm﹕ at android.app.ActivityThread.main(ActivityThread.java:5225)
12-26 20:34:03.318  11325-11325/samarth.learning.http I/dalvikvm﹕ at java.lang.reflect.Method.invokeNative(Native Method)
12-26 20:34:03.318  11325-11325/samarth.learning.http I/dalvikvm﹕ at java.lang.reflect.Method.invoke(Method.java:525)
12-26 20:34:03.318  11325-11325/samarth.learning.http I/dalvikvm﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
12-26 20:34:03.318  11325-11325/samarth.learning.http I/dalvikvm﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
12-26 20:34:03.318  11325-11325/samarth.learning.http I/dalvikvm﹕ at dalvik.system.NativeStart.main(Native Method)
12-26 20:34:03.318  11325-11325/samarth.learning.http I/dalvikvm﹕ [ 12-26 20:34:03.318 11325:11325 D/skia     ]
    --- decoder->decode returned false
12-26 20:34:03.318  11325-11325/samarth.learning.http D/AndroidRuntime﹕ Shutting down VM
12-26 20:34:03.318  11325-11325/samarth.learning.http W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415d78b0)
12-26 20:34:03.318  11325-11325/samarth.learning.http E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.OutOfMemoryError
            at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
            at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:429)
            at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:447)
            at samarth.learning.http.MainActivity$MyAsyncTask.onPostExecute(MainActivity.java:496)
            at samarth.learning.http.MainActivity$MyAsyncTask.onPostExecute(MainActivity.java:330)
            at android.os.AsyncTask.finish(AsyncTask.java:631)
            at android.os.AsyncTask.access$600(AsyncTask.java:177)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:5225)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
            at dalvik.system.NativeStart.main(Native Method)
Samarth Agarwal
  • 2,044
  • 8
  • 39
  • 79
  • **Why** do you encode and decode (still I must see **WHERE you decode** it) the image? – Phantômaxx Dec 26 '14 at 15:11
  • I need to do this because I a working in background using an `AsynTask`. I cannot update UI from there. So, instead, I am passing image data to `onPostExecution` and updating `ImageView` UI from there. The first code segment is encoding in `doInBackground()` and the second one is decoding in `onPostExecution()`. – Samarth Agarwal Dec 26 '14 at 15:15
  • You mean `onPostExecute()`. Still I see no need to `encode and decode` the image `to and from` Base64. **Simplify** your life: **1** - Download it somewhere on your sd. **2** - Load it from there. Much easier. – Phantômaxx Dec 26 '14 at 15:17
  • Ok, let's call it plan B. Any help on this one please. I am learning and want to do it. – Samarth Agarwal Dec 26 '14 at 15:19
  • http://stackoverflow.com/a/10868126/2649012 – Phantômaxx Dec 26 '14 at 15:25
  • Here, they are using `Bitmap` as the return type of the `doInBackground`. I have to use `String` as I am also passing other Strings, together as a `String Array` This is the reason I want to encode and decode. – Samarth Agarwal Dec 26 '14 at 15:30
  • You can pass a Bitmap, and save a lot of time. – Phantômaxx Dec 26 '14 at 15:37
  • I can only use one type of parameters. I am using String Array already. – Samarth Agarwal Dec 26 '14 at 15:39
  • myMethod(String[] strings, Bitmap picture, ...) – Phantômaxx Dec 26 '14 at 15:40
  • I am using `AsyncTask`, cannot use multiple parameters for `onPostExecute`. – Samarth Agarwal Dec 26 '14 at 15:42
  • Really? You can't call a method in `onPostExecute()`? For your reference: http://developer.android.com/reference/android/os/AsyncTask.html – Phantômaxx Dec 26 '14 at 16:01
  • 1
    As an alternative, there's still a way to pass a string after you saved a bitmap to your sd card: pass its **local path** (which is a string), instead of the bitmap. – Phantômaxx Dec 26 '14 at 16:05
  • That looks good, I'll try that as I don't know anything about that code. – Samarth Agarwal Dec 26 '14 at 16:06

0 Answers0