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

Android 从二进制数组在ImageView中显示图像,android,imageview,binary-data,Android,Imageview,Binary Data,我有一个web服务来检索转换为字符串的图像,并在我的android应用程序中转换回二进制数组。但不幸的是,在停止时出现强制关闭错误。 我使用此处解释的代码填充imageView: 这是我在C#中的web服务方法,它向我发送数据: public String GetPersonImage() { int PersonCode = 1; JPerson person = new JPerson(PersonCode);

我有一个web服务来检索转换为字符串的图像,并在我的android应用程序中转换回二进制数组。但不幸的是,在停止时出现强制关闭错误。
我使用此处解释的代码填充imageView:

这是我在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.
C#服务器正在向您发送一个base 64字符串,您正在尝试直接读取它的字节。首先需要对其进行解码,然后将其转换为字节数组

试试这个:

byte[] decodedBytes = Base64.decode(pic, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedBytes , 0, decodedBytes.length);
C#服务器正在向您发送一个base 64字符串,您正在尝试直接读取它的字节。首先需要对其进行解码,然后将其转换为字节数组

试试这个:

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

图片的大小是多少?看起来运行时正在尝试分配185mb…与问题无关,但您正在使用asynctask并在UIRead上调用get。您正在阻塞主线程,就像您在ui线程上发出请求一样。请不要这样做,并在onPostExecute方法中处理您的服务器响应。请阅读以下内容:图像的大小是多少?看起来运行时正在尝试分配185mb…与问题无关,但您正在使用asynctask并在UIRead上调用get。您正在阻塞主线程,就像您在ui线程上发出请求一样。请不要这样做,并在onPostExecute方法中处理您的服务器响应。阅读这篇文章可以解释185Mb的分配。base64可能包含被bitmapfactory解释为压缩图像大小的内容。这可以解释185Mb的分配。base64可能包含由bitmapfactory解释为压缩图像大小的内容。