1

我正在尝试制作一个浏览按钮,供用户选择图像。我使用以下代码:

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

3 回答 3

3
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;
   BitmapFactory.Options op = new BitmapFactory.Options();
   op.InSampleSize = 2;
   SherarClass.Uri = getRealPathFromURI(data.Data);
   using (Bitmap bitmap = BitmapFactory.DecodeFile(SherarClass.Uri, op))
   {
       imageView.SetImageBitmap(bitmap);
       GC.Collect();
   }
}

}

    public String getRealPathFromURI(Android.Net.Uri contentUri)
    {
        String[] projection = new String[] {Android.Provider.MediaStore.MediaColumnsConsts.Data };
        ContentResolver cr = this.ContentResolver;
        Android.Database.ICursor cursor = cr.Query(contentUri, projection,null, null, null);
        if (cursor != null && cursor.Count > 0)
        {
            cursor.MoveToFirst();
            int index =cursor.GetColumnIndex(Android.Provider.MediaStore.MediaColumnsConsts.Data);
            return cursor.GetString(index);
        }
        return null;
    }
于 2012-07-19T08:24:07.990 回答
0

尝试使用此代码。getPic 是一个按钮,用户必须单击它才能打开 MediaStore 以查看画廊类型查看器中的所有图像。

        getPic.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        Intent it = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(it, 101);
        }
    });

然后在 onActivityResult :

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) 
   {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case 101:
      if (resultCode == RESULT_OK) {
        Uri photoUri = data.getData();
        try {
            Bitmap currentImage = MediaStore.Images.Media.getBitmap
                                      (this.getContentResolver(), photoUri);

            imgView.setImageBitmap(currentImage);
  }

此代码将起作用,因为我已在我的个人项目中使用它。如果出现问题,请告诉我。

于 2012-07-14T10:32:39.800 回答
0

我会尝试使用以下活动结果:

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;
        using (var bitmap = MediaStore.Images.Media.GetBitmap(ContentResolver, selectedImageUri))
        {
            SherarClass.Uri = getRealPathFromURI(selectedImageUri);
            using (var dd = new BitmapDrawable(bitmap))
                imageView.SetImageDrawable(dd);
        }
    }
}

这会同时处理 theBitmap和不再需要的BitmapDrawable时候,然后就不需要自己处理了。

于 2012-07-14T11:17:09.613 回答