Skip to content Skip to sidebar Skip to footer

Android: How To Open Image From Gallery?

I've created a method in my app that saves my layout as an image and presents it in the gallery. I'm trying to open it but I'm having a hard time with all the tutorials and differe

Solution 1:

Intentintent=newIntent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, <unique_code>);

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == <unique_code>) {
        imageView.setImageBitmap(getPicture(data.getData()));
    }
}

publicstatic Bitmap getPicture(Uri selectedImage) {
    String[] filePathColumn = { MediaStore.Images.Media.DATA };
    Cursorcursor= getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
    cursor.moveToFirst();
    intcolumnIndex= cursor.getColumnIndex(filePathColumn[0]);
    StringpicturePath= cursor.getString(columnIndex);
    cursor.close();
    return BitmapFactory.decodeFile(picturePath);
}

Solution 2:

To open Gallery App from your code, call this

Intentintent=newIntent(Intent.ACTION_VIEW, Uri.parse(
     "content://media/internal/images/media")); 
     startActivity(intent);

Edit:

To open the recently saved image

publicvoidopenInGallery(String imageId) {
  Uriuri= MediaStore.Images.Media.EXTERNAL_CONTENT_URI.buildUpon().appendPath(imageId).build();
  Intentintent=newIntent(Intent.ACTION_VIEW, uri);
  startActivity(intent);
}

All you have to do is append the image id to the end of the path for the EXTERNAL_CONTENT_URI. Then launch an Intent with the View action, and the Uri.

The image id comes from querying the content resolver.

Post a Comment for "Android: How To Open Image From Gallery?"