Android - How to add an "Image Chooser" to an Android app

The URL shown has worked out well so far for letting me add an “Image Chooser” to my current Android application.

In case that URL every goes away, this code is needed to launch the Intent:

Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);

This method isn’t what I need for my purposes, but I’m using it as a starting point:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
          
        // String picturePath contains the path of selected Image

        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
}

As shown there, the picturePath is the important part. Well, that, and the code to convert a file path into an image.

Another thing you’ll need to do that isn’t shown in that tutorial is add the necessary permission to your Android application. You need to add this line to your AndroidManifest.xml file, at the same level as your <application> tag:

<!-- this seems to be needed for the 'image chooser', which lets user choose an image from their gallery -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

I’ll add more here as I learn it, but if you need to add an “Image Chooser” to an Android application, this approach seems to be working well so far. For more information, please see the URL that I’ve linked to.