Jack Bird Jack Bird - 2 months ago 17x
Android Question

Android: setImageUri failing

My application has an insert activity that has an image picker and the Uri from that is stored as a string ready to put into a database.

public void onActivityResult(int reqCode, int resCode, Intent data) {
if(resCode == RESULT_OK) {
if(reqCode == 1) {
imageUri = data.getData().toString();

imageUri is the String that is then later put in a database on a button press.

Once everything is inserted into the database the activity finishes to return the main activity which in the
method all the data from the database/table is retrieved in a List of objects that are then put into a custom array adapter to form my list view.

if(!uriString.equals("")) {

This is how the image is set inside the array adapter. The strange thing is that right after the insert activity closes the image is visible but as soon as I open another activity and return the image is blank. I get the error

resolveUri failed on bad bitmap uri: content://com.android.providers.media.documents/document/image%3A29938

I don't know why it can't be resolved because it is fine to start with but then it doesn't work.


Content URI's need to be parsed properly as they are not like normal filePath and hence we need some resolver. Here is an example how to approach this:

Changes to your code

if(!uriString.equals("")) {
     image.setImageURI(Uri.parse(getPathFromURI(<your app context>, Uri.parse("content://blah/blah/blah"))));

Method definition for the URI parser.

public String getPathFromURI(Context context, Uri contentUri) {
   Cursor mediaCursor = null;
   try { 
           String[] dataPath = { MediaStore.Images.Media.DATA };
           mediaCursor = context.getContentResolver().query(contentUri,  dataPath, null, null, null);
           int column_index = mediaCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
           return mediaCursor.getString(column_index);
   } finally {
       if (mediaCursor != null) {