Try to pick only one particular image from the SD card.I am able to pick images from gallery and Photos app in kikat.But not getting file path when i pick image from recents am getting file path as null.
Asked
Active
Viewed 1.4k times
10
-
Do you really need the imagepath? Are you uploading the image? or just want to show it in imageview? – Sunil Mishra Dec 11 '13 at 09:18
-
i just want to show it in a image view.. – Meher Dec 11 '13 at 09:23
-
Can you post your code as it would be easier for me to answer – Sunil Mishra Dec 11 '13 at 09:32
-
the same code i'm using as mentioned in the link.some guy even posted it as answer even. – Meher Dec 11 '13 at 10:00
-
@SunilMishra, How to store image path in sqlite database using the second method, parcelfiledescriptor (API >=19). I can store image path with method you have written for API <19 but i am not able to get the path from the second method. Please help. – Krups May 04 '16 at 07:40
1 Answers
33
This works for Kitkat
public class BrowsePictureActivity extends Activity{
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView imageView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browsepicture);
imageView = (ImageView)findViewById(R.id.imageView1);
((Button) findViewById(R.id.button1))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
if (Build.VERSION.SDK_INT < 19) {
selectedImagePath = getPath(selectedImageUri);
Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);
imageView.setImageBitmap(bitmap);
}
else {
ParcelFileDescriptor parcelFileDescriptor;
try {
parcelFileDescriptor = getContentResolver().openFileDescriptor(selectedImageUri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
imageView.setImageBitmap(image);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
/**
* helper to retrieve the path of an image URI
*/
public String getPath(Uri uri) {
if( uri == null ) {
return null;
}
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
return uri.getPath();
}
}
You need to add permission
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Mats Hofman
- 7,060
- 6
- 33
- 48

Sunil Mishra
- 3,796
- 1
- 27
- 40
-
The code is not same...check for the if else condition which checks for kitkat :P – Sunil Mishra Dec 11 '13 at 10:27
-
This works for me but it's extremely slow compared to what I use for getting images in all previous API levels. Do you know why it's so slow? Talking about 5 seconds until I can actually use the image. – Ben Kane Jan 27 '14 at 21:40
-
I'm with the same problem but the code is not working for the Kitkat :-( – Gabriel Augusto Jun 11 '14 at 18:55
-
HI @SunilMishra, can this be used to select an image in the gallery and insert in a sqlite database? Thanks – Jeongbebs Feb 24 '15 at 09:08
-
@MiguelRivera Yes you can store the bitmap into database, instead of setting it into imageview – Sunil Mishra Feb 25 '15 at 04:47
-
@SunilMishra, How to store image path in sqlite database using the second method, parcelfiledescriptor (API >=19). I can store image path with method you have written for API <19 but i am not able to get the path from the second method. Please help. – Krups May 04 '16 at 07:28