Choose a picture from the gallery only, not other application like Photos App

1.9k views Asked by At

I am trying to pick an image from the Android Gallery only, not other applications like Photos, file manager etc

I need a solution to open the Gallery App directly, or is it possible to use the Photos Application to pick image?

1) Choose from gallery

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent,CAMERA_CAPTURE_IMAGE_REQUEST_CODE);

2) onActivityResult result code

try {
    // bimatp factory
    BitmapFactory.Options options = new BitmapFactory.Options();
    // downsizing image as it throws OutOfMemory Exception for larger images
    options.inSampleSize = 2;
    final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),options);

    descimage.setImageBitmap(bitmap);

    bitmap.compress(CompressFormat.JPEG, 80, new FileOutputStream(new File(fileUri.getPath())));

    photostatus = 1;
    pbar.setVisibility(View.VISIBLE);

    txtbrowser.setEnabled(false);
    new upload().execute();

} catch (NullPointerException e) {
        e.printStackTrace();
}
2

There are 2 answers

0
BrentM On

You should be aware that Gallery no longer exists on some devices running Lollipop. The photos app is the replacement and it should have no problems handling the intent to select an image. Intent.ACTION_GET_CONTENT is usually recommended for selecting images, such as:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, ID);

Opening the gallery on devices that do have it installed is discussed here. Basically every different vendor may ship a different gallery app.

It is possible to launch a specific activity for an implicit intent (such as selecting an image) without showing the chooser dialog by using the PackageManager.queryIntentActivities() API to iterate all the available packages on the users device so you can explicitly launch the one you require.

0
Kartheek On

This intent allows you to pick the image from the default gallery.

 // in onCreate or any event where your want the user to  select a file
           Intent intent = new Intent();
           intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,
                                "Select Picture"), SELECT_PICTURE);

For receiving the selected image in onActivityResult()

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
            }
        }
    }

i got the solution from here