Access images from the Pictures app in the Android app

Like the iPhone has a UIImagePickerController that allows the user to access images stored on the device, do we have similar control in the Android SDK?

Thank.

+41
android android-image
Feb 15 '09 at 13:57
source share
3 answers

You can use startActivityForResult by passing in an Intent that describes the action you want to complete and the data source to perform the action.

Luckily for Android, there is an action in Android for choosing things: Intent.ACTION__PICK and a data source containing photos: android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI for images on the local device or android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI for images on the SD card.

A call to startActivityForResult passing in the select action and the image you want to select, for example:

 startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), SELECT_IMAGE); 

Then override onActivityResult to listen to the user by making a choice.

 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == SELECT_IMAGE) if (resultCode == Activity.RESULT_OK) { Uri selectedImage = data.getData(); // TODO Do something with the select image URI } } 

Once you have a Uri image, you can use it to access the image and do whatever you need with it.

+83
Feb 15 '09 at 16:26
source share

You can also do:

 Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); photoPickerIntent.setType("image/*"); startActivityForResult(photoPickerIntent, 1); 

This will display images in all repositories.

+22
Oct 08 '11 at 12:10
source share

Just update the answer received by Reto. You can do this to scale the image:

 private String getPath(Uri uri) { String[] data = { MediaStore.Images.Media.DATA }; CursorLoader loader = new CursorLoader(context, uri, data, null, null, null); Cursor cursor = loader.loadInBackground(); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } 
+1
Jan 30 '13 at 13:46
source share



All Articles