gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:19:27 26 4
gpt4 key购买 nike

我有一个网络服务来检索图像,这些图像被转换为​​字符串,并在我的 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 变量包含来自网络服务的数据。这是我的日志猫:

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

关于android - 从二进制数组在 ImageView 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19956070/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com