Suppose you want to get a picture from the gallery.
Then we write the following:
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT); photoPickerIntent.setType("image/*"); startActivityForResult(photoPickerIntent, 1);
This will launch the gallery. When you select a photo, the onActivityResult () method will work, so we override it:
protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch(requestCode) { case 1: { if (resultCode == RESULT_OK) { Uri chosenImageUri = data.getData(); } break; } } }
In the chosenImageUri object there is a path to the gallery object, it looks like this: media / xcvbcxvb / sdfadf / xvb / 12332
In order to get the path to the SD card from this nonsense we write:
final Cursor cursor = getContentResolver().query( chosenImageUri, null, null, null, null ); cursor.moveToFirst(); final String filePath = cursor.getString(0); cursor.close();
Voila, in the filePath object is the full path to the file we need