Question

I have a web service to retrieve images which are converted to string and in my android app convert back to binary array. but unfortunately in stops with a force close error.
I'm using the code explained here to fill the imageView:
display image from byteArray

This is my web service method in C# which sends me the data:

public String GetPersonImage()
        {
            int PersonCode = 1;
            JPerson person = new JPerson(PersonCode);
            Image personImage = person.PersonImage;
            if (personImage == null)
                return "Nothing";
            using (MemoryStream ms = new MemoryStream())
            {
                personImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                return Convert.ToBase64String(ms.ToArray());
            }
        }

And this is how I'm trying to put my data in an ImageView:

            String pic = t.execute().get();
            byte[] picData = pic.getBytes();
            ImageView imgPerson = (ImageView) findViewById(R.id.imgPerson);
            Bitmap bitmap = BitmapFactory.decodeByteArray(picData, 0, picData.length);
            imgPerson.setImageBitmap(bitmap);

the pic variable contains the data from web service. And this is my log cat:

11-13 14:08:23.092: D/dalvikvm(29229): GC_FOR_ALLOC freed 2290K, 41% free 8004K/13420K, paused 27ms, total 27ms
11-13 14:08:23.092: I/dalvikvm-heap(29229): Forcing collection of SoftReferences for 185326656-byte allocation
11-13 14:08:23.143: D/dalvikvm(29229): GC_BEFORE_OOM freed 9K, 41% free 7994K/13420K, paused 42ms, total 43ms
11-13 14:08:23.143: E/dalvikvm-heap(29229): Out of memory on a 185326656-byte allocation.
11-13 14:08:23.143: I/dalvikvm(29229): "main" prio=5 tid=1 RUNNABLE
11-13 14:08:23.143: I/dalvikvm(29229):   | group="main" sCount=0 dsCount=0 obj=0x40a729a0 self=0x2a00bba8
11-13 14:08:23.143: I/dalvikvm(29229):   | sysTid=29229 nice=0 sched=0/0 cgrp=apps handle=1073849308
11-13 14:08:23.143: I/dalvikvm(29229):   | state=R schedstat=( 1210556247 2173314492 1073 ) utm=93 stm=28 core=0
11-13 14:08:23.143: I/dalvikvm(29229):   at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
11-13 14:08:23.151: I/dalvikvm(29229):   at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:428)
11-13 14:08:23.151: I/dalvikvm(29229):   at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:446)
11-13 14:08:23.151: I/dalvikvm(29229):   at com.example.shareholders.PersonnelInfo.clickHandler(PersonnelInfo.java:116)
11-13 14:08:23.151: I/dalvikvm(29229):   at java.lang.reflect.Method.invokeNative(Native Method)
11-13 14:08:23.151: I/dalvikvm(29229):   at java.lang.reflect.Method.invoke(Method.java:511)
11-13 14:08:23.151: I/dalvikvm(29229):   at android.view.View$1.onClick(View.java:3594)
11-13 14:08:23.151: I/dalvikvm(29229):   at android.view.View.performClick(View.java:4204)
11-13 14:08:23.151: I/dalvikvm(29229):   at android.view.View$PerformClick.run(View.java:17355)
11-13 14:08:23.151: I/dalvikvm(29229):   at android.os.Handler.handleCallback(Handler.java:725)
11-13 14:08:23.151: I/dalvikvm(29229):   at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 14:08:23.151: I/dalvikvm(29229):   at android.os.Looper.loop(Looper.java:137)
11-13 14:08:23.151: I/dalvikvm(29229):   at android.app.ActivityThread.main(ActivityThread.java:5041)
11-13 14:08:23.151: I/dalvikvm(29229):   at java.lang.reflect.Method.invokeNative(Native Method)
11-13 14:08:23.151: I/dalvikvm(29229):   at java.lang.reflect.Method.invoke(Method.java:511)
11-13 14:08:23.151: I/dalvikvm(29229):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-13 14:08:23.151: I/dalvikvm(29229):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-13 14:08:23.151: I/dalvikvm(29229):   at dalvik.system.NativeStart.main(Native Method)
11-13 14:08:23.161: D/skia(29229): --- decoder->decode returned false
11-13 14:08:23.161: D/AndroidRuntime(29229): Shutting down VM
11-13 14:08:23.161: W/dalvikvm(29229): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
11-13 14:08:23.182: E/AndroidRuntime(29229): FATAL EXCEPTION: main
11-13 14:08:23.182: E/AndroidRuntime(29229): java.lang.IllegalStateException: Could not execute method of the activity
11-13 14:08:23.182: E/AndroidRuntime(29229):    at android.view.View$1.onClick(View.java:3599)
11-13 14:08:23.182: E/AndroidRuntime(29229):    at android.view.View.performClick(View.java:4204)
11-13 14:08:23.182: E/AndroidRuntime(29229):    at android.view.View$PerformClick.run(View.java:17355)
11-13 14:08:23.182: E/AndroidRuntime(29229):    at android.os.Handler.handleCallback(Handler.java:725)
11-13 14:08:23.182: E/AndroidRuntime(29229):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 14:08:23.182: E/AndroidRuntime(29229):    at android.os.Looper.loop(Looper.java:137)
11-13 14:08:23.182: E/AndroidRuntime(29229):    at android.app.ActivityThread.main(ActivityThread.java:5041)
11-13 14:08:23.182: E/AndroidRuntime(29229):    at java.lang.reflect.Method.invokeNative(Native Method)
11-13 14:08:23.182: E/AndroidRuntime(29229):    at java.lang.reflect.Method.invoke(Method.java:511)
11-13 14:08:23.182: E/AndroidRuntime(29229):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-13 14:08:23.182: E/AndroidRuntime(29229):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-13 14:08:23.182: E/AndroidRuntime(29229):    at dalvik.system.NativeStart.main(Native Method)
11-13 14:08:23.182: E/AndroidRuntime(29229): Caused by: java.lang.reflect.InvocationTargetException
11-13 14:08:23.182: E/AndroidRuntime(29229):    at java.lang.reflect.Method.invokeNative(Native Method)
11-13 14:08:23.182: E/AndroidRuntime(29229):    at java.lang.reflect.Method.invoke(Method.java:511)
11-13 14:08:23.182: E/AndroidRuntime(29229):    at android.view.View$1.onClick(View.java:3594)
11-13 14:08:23.182: E/AndroidRuntime(29229):    ... 11 more
11-13 14:08:23.182: E/AndroidRuntime(29229): Caused by: java.lang.OutOfMemoryError
11-13 14:08:23.182: E/AndroidRuntime(29229):    at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
11-13 14:08:23.182: E/AndroidRuntime(29229):    at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:428)
11-13 14:08:23.182: E/AndroidRuntime(29229):    at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:446)
11-13 14:08:23.182: E/AndroidRuntime(29229):    at com.example.shareholders.PersonnelInfo.clickHandler(PersonnelInfo.java:116)
11-13 14:08:23.182: E/AndroidRuntime(29229):    ... 14 more
11-13 14:13:23.284: I/Process(29229): Sending signal. PID: 29229 SIG: 9
11-13 14:13:23.751: E/Trace(31566): error opening trace file: No such file or directory (2)
11-13 14:13:23.791: I/METHOD INFO(31566): AppMenu.onCreate():21
11-13 14:13:23.912: D/dalvikvm(31566): GC_CONCURRENT freed 61K, 8% free 2752K/2964K, paused 15ms+11ms, total 77ms
11-13 14:13:24.051: D/gralloc_goldfish(31566): Emulator without GPU emulation detected.
Was it helpful?

Solution

The C# server is sending you a base 64 string, and you're trying to read its bytes directly. You first need to decode it and then convert to a byte array.

Try this:

byte[] decodedBytes = Base64.decode(pic, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedBytes , 0, decodedBytes.length);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top