1

I'm having trouble getting capture.captureImage to work. Using Cordova 3.5.0, org.apache.cordova.file-1.3.0, org.apache.cordova.media-capture-0.3.3. Debugging in Eclipse shows that

that.cordova.getActivity().getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)

in onActivityResult returns uri = content://media/external/images/media/nnnn but

OutputStream os = that.cordova.getActivity().getContentResolver().openOutputStream(uri)

throws a FileNotFoundException

dp22193
  • 43
  • 1
  • 5

2 Answers2

1

It appears that the issue is that the uri returned by the insert method is not properly resolved by openOutputStream. What worked for me was instead of calling openOutputStream, do the following:

String dfname = getRealPathFromURI(uri);
File df = new File(dfname);
File dfolder = df.getParentFile();
if(!dfolder.exists()) dfolder.mkdirs();
if(!df.exists()) df.createNewFile();
FileOutputStream os = new FileOutputStream(df);

private String getRealPathFromURI(Uri contentURI) {
    Cursor cursor = this.cordova.getActivity().getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) { // Source is Dropbox or other similar local file path
        return contentURI.getPath();
    } else { 
        cursor.moveToFirst(); 
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
        return cursor.getString(idx); 
    }
}

You'll need to import java.io.FileOutputStream, if not already done. I thank Isaac Zais for his answer that pointed me in the right direction and for the getRealPathFromURI code.

It would be great if a Cordova developer could explain if this is a bug in org.apache.cordova.media-capture or somewhere else or if there is a way to get this to work without having to modify the latest version of the plugin.

Tested on Samsung Galaxy S5, Android 4.4.4.

Community
  • 1
  • 1
dp22193
  • 43
  • 1
  • 5
1

I don't have enough reputation to comment on your answer, but dp22193's patch works great! Please see https://issues.apache.org/jira/browse/CB-7768 for the bug report and the patch attached to it.

Lifz
  • 690
  • 6
  • 10