ユーザーが画像を選択できるように、参照ボタンを作成しようとしています。次のコードを使用します。
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 例外をスローした後、1 回目と 2 回目は機能します。
スタックトレース :
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)