Repository: cordova-plugin-media-capture Updated Branches: refs/heads/master 21bb35e46 -> cd7d40d5d
CB-11995 (android) Do not pass a file URI to the camera Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media-capture/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media-capture/commit/108c2eff Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media-capture/tree/108c2eff Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media-capture/diff/108c2eff Branch: refs/heads/master Commit: 108c2eff02700c90103e63c09e8144d965aff03f Parents: 21bb35e Author: Alexander Sorokin <alexander.soro...@akvelon.com> Authored: Tue Oct 11 18:04:32 2016 +0300 Committer: Alexander Sorokin <alexander.soro...@akvelon.com> Committed: Tue Oct 11 18:04:32 2016 +0300 ---------------------------------------------------------------------- src/android/Capture.java | 74 +++++++++++-------------------------------- 1 file changed, 18 insertions(+), 56 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-media-capture/blob/108c2eff/src/android/Capture.java ---------------------------------------------------------------------- diff --git a/src/android/Capture.java b/src/android/Capture.java index 73781b8..1f7030b 100644 --- a/src/android/Capture.java +++ b/src/android/Capture.java @@ -44,6 +44,7 @@ import org.json.JSONObject; import android.Manifest; import android.app.Activity; +import android.content.ContentResolver; import android.content.ContentValues; import android.content.Intent; import android.content.pm.PackageManager; @@ -78,6 +79,7 @@ public class Capture extends CordovaPlugin { private final PendingRequests pendingRequests = new PendingRequests(); private int numPics; // Number of pictures before capture activity + private Uri imageUri; // public void setContext(Context mCtx) // { @@ -266,16 +268,13 @@ public class Capture extends CordovaPlugin { Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); - // Specify file so that large image is captured and returned - File photo = new File(getTempDirectoryPath(), "Capture.jpg"); - try { - // the ACTION_IMAGE_CAPTURE is run under different credentials and has to be granted write permissions - createWritableFile(photo); - } catch (IOException ex) { - pendingRequests.resolveWithFailure(req, createErrorObject(CAPTURE_INTERNAL_ERR, ex.toString())); - return; - } - intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); + ContentResolver contentResolver = this.cordova.getActivity().getContentResolver(); + ContentValues cv = new ContentValues(); + cv.put(MediaStore.Images.Media.MIME_TYPE, IMAGE_JPEG); + imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv); + LOG.d(LOG_TAG, "Taking a picture and saving to: " + imageUri.toString()); + + intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri); this.cordova.startActivityForResult((CordovaPlugin) this, intent, req.requestCode); } @@ -377,54 +376,17 @@ public class Capture extends CordovaPlugin { } public void onImageActivityResult(Request req) { - // For some reason if I try to do: - // Uri data = intent.getData(); - // It crashes in the emulator and on my phone with a null pointer exception - // To work around it I had to grab the code from CameraLauncher.java - try { - // Create entry in media store for image - // (Don't use insertImage() because it uses default compression setting of 50 - no way to change it) - ContentValues values = new ContentValues(); - values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, IMAGE_JPEG); - Uri uri = null; - try { - uri = this.cordova.getActivity().getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); - } catch (UnsupportedOperationException e) { - LOG.d(LOG_TAG, "Can't write to external media storage."); - try { - uri = this.cordova.getActivity().getContentResolver().insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values); - } catch (UnsupportedOperationException ex) { - LOG.d(LOG_TAG, "Can't write to internal media storage."); - pendingRequests.resolveWithFailure(req, createErrorObject(CAPTURE_INTERNAL_ERR, "Error capturing image - no media storage found.")); - return; - } - } - FileInputStream fis = new FileInputStream(getTempDirectoryPath() + "/Capture.jpg"); - OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri); - byte[] buffer = new byte[4096]; - int len; - while ((len = fis.read(buffer)) != -1) { - os.write(buffer, 0, len); - } - os.flush(); - os.close(); - fis.close(); - - // Add image to results - req.results.put(createMediaFile(uri)); + // Add image to results + req.results.put(createMediaFile(imageUri)); - checkForDuplicateImage(); + checkForDuplicateImage(); - if (req.results.length() >= req.limit) { - // Send Uri back to JavaScript for viewing image - pendingRequests.resolveWithSuccess(req); - } else { - // still need to capture more images - captureImage(req); - } - } catch (IOException e) { - e.printStackTrace(); - pendingRequests.resolveWithFailure(req, createErrorObject(CAPTURE_INTERNAL_ERR, "Error capturing image.")); + if (req.results.length() >= req.limit) { + // Send Uri back to JavaScript for viewing image + pendingRequests.resolveWithSuccess(req); + } else { + // still need to capture more images + captureImage(req); } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@cordova.apache.org For additional commands, e-mail: commits-h...@cordova.apache.org