How to open the default gallery from android and receive the picked uri of the picture?

Al Phaba

Is it possible to call the default gallery from the code, so that the user will be shown all the pictures of his gallery to select one picture. As a result I need to receive the path of the selected picture so that i can do my processing with it.

Thanks

AAnkit

Use below code to create and fire a intent to start Gallery

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

and below line in onActivityResult(int requestCode, int resultCode, Intent data) method to fetch URI

Uri dataUri = data.getData(); //Image URI

You would most probably need below method to convert this Image URI to small size bitmap . if showing image in your display, as without it , it could result in OOM exception

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 140;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp / 2 < REQUIRED_SIZE
           || height_tmp / 2 < REQUIRED_SIZE) {
            break;
        }
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to retrieve a picture from gallery in android?

Action.Picker returns invalid/wrong Uri (How to get path or byte[] from multiple picked gallery img)

How to view image from android gallery using Image URI

resize image picked from gallery

SecurityException reading picture from Android gallery

Android RelativeLayout Background Picture from Gallery

Android open gallery from folder

How to get the thumbnail of an Android gallery picture, in NativeScript?

How to get Image URI from Gallery?

Looking for a open source picture gallery

Image Picked from Gallery ImageView Disappeard

Flutter display image picked from gallery

saving image picked from gallery for future use

Open gallery app from Android Intent

How to open picture from resources?

Move a picture from gallery to drawable using code. [android studio]

Ionic-Upload picture from camera or gallery in Android

How to store image uri from gallery in database for loading later in Xamarin.Android?

How can I get the file name from an image picked from the gallery?

How to view an image with default gallery in Android 8.0?

Receive video intent from gallery or browsable file manager in Android

Android how to open a specific director in Gallery

Android - how to open image in gallery with delete option

How can I get the width and height of an Image picked from gallery after cropping in it Image view

How to open a file from Android content URI in NativeScript?

Xamarin Android - How to select a video app to open from a URI?

How do I save a picture from the gallery in the application?

How to let the user to choose from Gallery or take picture option in Cordova

How to open default gallery app with particular album or folder?