在二进制数组中显示ImageView中的图像

时间:2013-11-13 14:17:21

标签: android imageview binary-data

我有一个Web服务来检索转换为字符串的图像,并在我的Android应用程序中转换回二进制数组。但不幸的是在停止时出现了力量关闭错误 我正在使用这里解释的代码来填充imageView:
display image from byteArray

这是我在C#中的Web服务方法,它向我发送数据:

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());
            }
        }

这就是我试图将数据放入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);

pic变量包含来自Web服务的数据。这是我的原木猫:

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.

1 个答案:

答案 0 :(得分:5)

C#服务器正在向您发送一个基本的64字符串,并且您正在尝试直接读取其字节。首先需要解码它,然后转换为字节数组。

试试这个:

byte[] decodedBytes = Base64.decode(pic, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedBytes , 0, decodedBytes.length);