
Gallery Intent Using Default Gallery Application
If you want to start a gallery application that uses the device's default gallery app, you just need to call the Intent with MediaStore.Image.Media.EXTERNAL_CONTENT_URI and do Activity.startActivityForResult() instead of Activity.startActivity() on that intent. This way, you can retrieve the image path that the user has selected.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Start intent to get image using device's default gallery app. | |
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); | |
startActivityForResult(intent, GALLERY_INTENT); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (resultCode == RESULT_OK) { | |
if (requestCode == GALLERY_INTENT || requestCode == GALLERY_CHOOSER_INTENT) { | |
// Get the filepath and display on imageview. | |
String filepath = getGalleryImagePath(data); | |
// Check if the specified image exists. | |
if (!new File(filepath).exists()) { | |
Toast.makeText(this, "Image does not exist.", Toast.LENGTH_SHORT).show(); | |
} else { | |
// Display image in ImageView. | |
Bitmap img = BitmapFactory.decodeFile(filepath); | |
mImage.setImageBitmap(img); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Using the Intent data that the Gallery app returns, this method will | |
* retrive the filepath location of the image that the user have | |
* selected. | |
* | |
* Known issues: This does not work for getting images from Google Drive and | |
* Pisca. | |
*/ | |
public String getGalleryImagePath(Intent data) { | |
Uri imgUri = data.getData(); | |
String filePath = ""; | |
if (data.getType() == null) { | |
// For getting images from default gallery app. | |
String[] filePathColumn = { MediaStore.Images.Media.DATA }; | |
Cursor cursor = getContentResolver().query(imgUri, filePathColumn, null, null, null); | |
cursor.moveToFirst(); | |
int columnIndex = cursor.getColumnIndex(filePathColumn[0]); | |
filePath = cursor.getString(columnIndex); | |
cursor.close(); | |
} else if (data.getType().equals("image/jpeg") || data.getType().equals("image/png")) { | |
// For getting images from dropbox or any other gallery apps. | |
filePath = imgUri.getPath(); | |
} | |
return filePath; | |
} |
Gallery Intent Using with Ability to Choose Gallery Application
Just automatically opening up the default gallery application does not give much choice for your users, hence I will describe how you can give users more flexibility when selecting images on the device. Simply when you create the intent, first specify what type of file you would like to import using MIME type and telling that you want to get some form of content using the constant Intent.ACTION_GET_CONTENT and then finally invoke Activity.startActivityForResult().
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Start intent to get image using using any image viewing app available on the device. | |
Intent intent = new Intent(); | |
intent.setType("image/*"); | |
intent.setAction(Intent.ACTION_GET_CONTENT); | |
startActivityForResult(intent, GALLERY_CHOOSER_INTENT); |
You can view my Android Camera Intent example HERE.
Relevant files are:
src/com/choiboi/apiexamples/gallery/GalleryMainActivity.java
res/layout/activity_gallery_main.xml
Enjoy!!