为什么 imageview 返回 null 或不从图库中检索选定的照片? (Xamarin VS 2015 Android)

Why is the imageview returning null or not retrieving the selected photo from gallery? (Xamarin VS 2015 Android)

这已经让我紧张了将近一天,因为我似乎无法弄清楚为什么在我 select 图库中的图像(我的 android phone)。尽管图像下方的工具(按钮、文本视图等)向下移动,但它只给我一张没有实际图像的完整空白照片。我的代码基于这里 Selecting a Gallery Image 和其他线程,但仍然给了我一个 null。

这是我的代码:

AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-permission android:name="android.permission.CAMERA" android:required="false"/>

Main.axml

<ImageView
            p1:src="@android:drawable/ic_menu_gallery"
            p1:layout_width="fill_parent"
            p1:layout_height="300.0dp"
            p1:id="@+id/imageView1" />
<Button
            p1:text="Attach a photo"
            p1:layout_width="wrap_content"
            p1:layout_height="wrap_content"
            p1:layout_gravity="center_horizontal"
            p1:id="@+id/button1" />

MainActivity.cs(我省略了一些与我的问题无关的代码。)

public static readonly Int32 REQUEST_CAMERA = 0;
public static readonly Int32 SELECT_FILE = 1;
private ImageView _imageView;

protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            if (IsThereAnAppToTakePictures())
            {
                CreateDirectoryForPictures();

                Button button = FindViewById<Button>(Resource.Id.button1);
                _imageView = FindViewById<ImageView>(Resource.Id.imageView1);
                button.Click += ButtonOnClick;
            }
        }

private void ButtonOnClick(object sender, EventArgs eventArgs)
        {
            String[] items = { "Take Photo", "Choose from Library", "Cancel" };

            using (var dialogBuilder = new AlertDialog.Builder(this))
            {
                dialogBuilder.SetTitle("Add Photo");
                dialogBuilder.SetItems(items, (d, args) => {
                    //Take photo
                    if (args.Which == 0)
                    {
                        TakeAPicture(sender, eventArgs);
                    }
                    //Choose from gallery
                    else if (args.Which == 1)
                    {
                        Intent intent = new Intent(Intent.ActionPick, MediaStore.Images.Media.ExternalContentUri);
                        intent.SetType("image/*");
                        intent.SetAction(Intent.ActionGetContent);
                        StartActivityForResult(Intent.CreateChooser(intent, "Select Picture"), SELECT_FILE);
                    }
                });

                dialogBuilder.Show();
            }
        }

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if (resultCode == Result.Ok)
            {
                if (requestCode == REQUEST_CAMERA)
                {
                    Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
                    Android.Net.Uri contentUri = Android.Net.Uri.FromFile(App._file);
                    mediaScanIntent.SetData(contentUri);
                    SendBroadcast(mediaScanIntent);

                    // Display in ImageView. We will resize the bitmap to fit the display.
                    // Loading the full sized image will consume to much memory
                    // and cause the application to crash.

                    int height = Resources.DisplayMetrics.HeightPixels;
                    int width = _imageView.Height;
                    App.bitmap = App._file.Path.LoadAndResizeBitmap(width, height);
                    if (App.bitmap != null)
                    {
                        _imageView.SetImageBitmap(App.bitmap);
                        App.bitmap = null;
                    }

                    // Dispose of the Java side bitmap.
                    GC.Collect();
                }
                else if ((requestCode == SELECT_FILE) && (data != null))
                {
                    Android.Net.Uri uri = data.Data;
                    _imageView.SetImageURI(uri);
                }
            }

        }

我早些时候在 Xamarin 的论坛上发布了这个,但他们似乎也不知道(?)或者我的问题含糊不清(?)。

您在 Activity class 级别定义的 _imageViewnullOnActivityResult 方法中。

您可以通过以下方式设置:

_imageView = FindViewById<ImageView> (Resource.Id.imageView1);

您还可以直接使用 Intent 参数中的 data.Data 作为 Uri,而不必创建另一个 Uri 对象来保存它。

试试这个代码:

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
   base.OnActivityResult(requestCode, resultCode, data);
   _imageView = FindViewById<ImageView> (Resource.Id.imageView1);

   if (resultCode == Result.Ok)
   ...

   else if ((requestCode == SELECT_FILE) && (data != null))
   {
        _imageView.SetImageURI(data.Data);
   }