Mono for Android在BitmapFactory.DecodeStream上的退出码为255
本文关键字:退出 DecodeStream for Android BitmapFactory Mono | 更新日期: 2023-09-27 18:09:27
我使用Mono for Android(本文发布时的最新版本)和Visual Studio插件来构建Android应用程序。它针对API Level 8, Android 2.2框架。
该应用程序在运行Android 2.2.2版本的摩托罗拉Droid上运行良好在运行Android 2.3.3的摩托罗拉Droid X2上,调试器几乎没有输出就崩溃了
唯一的输出是:程序'Mono'已经退出,代码为255 (0xff)。
崩溃发生在这个方法中以using (Bitmap...
public static Drawable GetDrawable(string url) {
try {
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Bitmap bitmap = Android.Graphics.BitmapFactory.DecodeStream(response.GetResponseStream())) {
Drawable image = (Drawable)(new BitmapDrawable(bitmap));
return image;
}
}
catch {
return null;
}
}
如果我在那行上设置了一个断点,它会正确地中断,但我找不到任何错误。如果我在那行之后设置了一个断点,调试器就会分离,应用程序就会强制退出。
我有一个类似的方法,从JSON返回一个对象,它工作得很好。所以,我很确定它与动态位图创建有关,但到目前为止,我已经尝试了我能想到的所有方法。
更新:
我只是在一个小的,独立的项目中重现了这个问题:DrawableTest.zip
完整代码:
using System;
using System.Net;
using System.Threading;
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Graphics;
using Android.Graphics.Drawables;
namespace DrawableTest {
[Activity(Label = "DrawableTest", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity {
ImageView mImage;
Button mButton;
public const string mImageUrl = "http://i.stpost.com/erez4/erez?src=ProductImages/3576U_02.tif&tmp=MediumLargeG4&redirect=0&headers=proxy";
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
mImage = FindViewById<ImageView>(Resource.Id.MyImage);
mButton = FindViewById<Button>(Resource.Id.MyButton);
mButton.Click += new EventHandler(mButton_Click);
}
void mButton_Click(object sender, EventArgs e) {
ThreadPool.QueueUserWorkItem(o => AsyncImageLoad());
}
private Drawable GetDrawable(string url) {
try {
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Bitmap bitmap = Android.Graphics.BitmapFactory.DecodeStream(response.GetResponseStream())) {
Drawable image = new BitmapDrawable(bitmap);
return image;
}
}
catch (Exception e) {
return null;
}
}
private void AsyncImageLoad() {
Drawable image = GetDrawable(mImageUrl);
RunOnUiThread(() => {
mImage.SetImageDrawable(image);
});
}
}
}
这可能是流映射代码中的一个bug,类似于:http://bugzilla.xamarin.com/show_bug.cgi?id=1054
应该在下一个版本(1.9)中修复。x,可能).
作为一种解决方法,尝试将response.GetResponseStream()
复制到System.IO中。MemoryStream,然后传递MemoryStream给BitmapFactory.DecodeStream()
。
我的想法是对的,但是方法是错的。用BitmapFactory.DecodeByteArray()
代替BitmapFactory.DecodeStream()
。以下代码在您的DrawableTest.zip
应用程序中为我工作:
private Drawable GetDrawable(string url)
{
try {
HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(url);
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
using (Stream responseStream = response.GetResponseStream()) {
MemoryStream workaround = new MemoryStream();
responseStream.CopyTo(workaround);
Bitmap bitmap = Android.Graphics.BitmapFactory.DecodeByteArray (
workaround.GetBuffer (), 0, (int) workaround.Length);
Drawable image = new BitmapDrawable(bitmap);
return image;
}
}
catch (Exception e) {
Log.Error("DrawableTest", "Exception: " + e.Message);
return null;
}
}
注意using
块,以确保资源被处置。
尝试在没有调试器的情况下运行(Ctrl-F5),然后检查Android调试日志是否有异常:
http://android.xamarin.com/Documentation/Guides/Android_Debug_Log