Why is the imageview returning null or not retrieving the selected photo from gallery?

PamPam PHMember
edited April 2016 in Xamarin.Android

This is stressing me out for a while now because I cannot seem to figure out why the image isn't showing up after I select an image from the gallery. It only gives me a full blank photo without the actual image although the tools (button, textview, etc.) below the image move down. I based my code here https://developer.xamarin.com/recipes/android/data/files/selecting_a_gallery_image/ and in other threads but still gives me a null.

Note: I am new to this so this may sound dumb for you.

Here are my codes:

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 (I omitted some unrelated codes to my question.)

public static readonly Int32 REQUEST_CAMERA = 0;
        public static readonly int 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);
                }
            }

        }

Best Answer

Answers

  • endaruliuendaruliu USMember

    I have the same problem. How did you solve this?

  • PamPam PHMember

    @endaruliu Sorry for the late reply. I just resized the image before setting it in the imageview using Bitmap helpers.

Sign In or Register to comment.