我正在尝试制作一个浏览按钮,供用户选择图像。我使用以下代码:
void BrowseButton_Click(object sender, EventArgs e)
{
Intent intent = new Intent(); intent.SetType("image/*");
intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(intent, "Select Picture"), 1);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Android.Content.Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode == Result.Ok && requestCode == 1)
{
ImageView imageView = FindViewById<ImageView>(Resource.Id.MainimageView);
Android.Net.Uri selectedImageUri = data.Data;
var bitmap = MediaStore.Images.Media.GetBitmap(ContentResolver, data.Data);
SherarClass.Uri = getRealPathFromURI(data.Data);
BitmapDrawable dd = new BitmapDrawable(bitmap);
imageView.SetImageDrawable(dd);
bitmap.Dispose();
}
}
在它引发 Java.Long.Error 异常之后,它第一次和第二次工作。
堆栈跟踪 :
at Android.Runtime.JNIEnv.CallStaticObjectMethod (IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00000] in <filename unknown>:0
at Android.Provider.MediaStore+Images+Media.GetBitmap (Android.Content.ContentResolver cr, Android.Net.Uri url) [0x00000] in <filename unknown>:0
at Scannet.Activity1.OnActivityResult (Int32 requestCode, Result resultCode, Android.Content.Intent data) [0x00000] in <filename unknown>:0
--- End of managed exception stack trace ---
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:556)
at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:706)
at scannet.Activity1.n_onActivityResult(Native Method)
at scannet.Activity1.onActivityResult(Activity1.java:37)
at android.app.Activity.dispatchActivityResult(Activity.java:4649)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2996)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3043)
at android.app.ActivityThread.access$1100(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1188)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)