This is an automated email from the ASF dual-hosted git repository. erisu pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/cordova-plugin-media-capture.git
The following commit(s) were added to refs/heads/master by this push: new 69bb93d fix(android): prevent app crash caused by NPE on intent data or mediaFile (#232) 69bb93d is described below commit 69bb93da435b3e7d82bd958a999ceceacef530c5 Author: Alexis THOMAS <ath0...@users.noreply.github.com> AuthorDate: Thu Aug 3 09:40:52 2023 +0200 fix(android): prevent app crash caused by NPE on intent data or mediaFile (#232) * fix(android): standard way to get video uri from intent see #210, and follow doc https://developer.android.com/training/camera/videobasics#TaskVideoView (removes partial changes done in #13 for old android and cordova versions) * style(android): cleanup imports and remove unused method * fix(android): prevent NPE with null checks on intent data and mediaFile --- src/android/Capture.java | 104 +++++++++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 49 deletions(-) diff --git a/src/android/Capture.java b/src/android/Capture.java index f008eba..43bcc57 100644 --- a/src/android/Capture.java +++ b/src/android/Capture.java @@ -20,24 +20,19 @@ package org.apache.cordova.mediacapture; import java.io.File; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; -import android.content.ActivityNotFoundException; -import android.os.Build; -import android.os.Bundle; - -import org.apache.cordova.file.FileUtils; -import org.apache.cordova.file.LocalFilesystemURL; - import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaPlugin; import org.apache.cordova.LOG; import org.apache.cordova.PermissionHelper; import org.apache.cordova.PluginManager; +import org.apache.cordova.file.FileUtils; +import org.apache.cordova.file.LocalFilesystemURL; import org.apache.cordova.mediacapture.PendingRequests.Request; import org.json.JSONArray; import org.json.JSONException; @@ -45,6 +40,7 @@ import org.json.JSONObject; import android.Manifest; import android.app.Activity; +import android.content.ActivityNotFoundException; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Intent; @@ -54,6 +50,8 @@ import android.database.Cursor; import android.graphics.BitmapFactory; import android.media.MediaPlayer; import android.net.Uri; +import android.os.Build; +import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; @@ -285,17 +283,6 @@ public class Capture extends CordovaPlugin { } } - private String getTempDirectoryPath() { - File cache = null; - - // Use internal storage - cache = cordova.getActivity().getCacheDir(); - - // Create the cache directory if it doesn't exist - cache.mkdirs(); - return cache.getAbsolutePath(); - } - /** * Sets up an intent to capture images. Result handled by onActivityResult() */ @@ -318,11 +305,6 @@ public class Capture extends CordovaPlugin { this.cordova.startActivityForResult((CordovaPlugin) this, intent, req.requestCode); } - private static void createWritableFile(File file) throws IOException { - file.createNewFile(); - file.setWritable(true, false); - } - /** * Sets up an intent to capture video. Result handled by onActivityResult() */ @@ -399,8 +381,19 @@ public class Capture extends CordovaPlugin { public void onAudioActivityResult(Request req, Intent intent) { // Get the uri of the audio clip Uri data = intent.getData(); - // create a file object from the uri - req.results.put(createMediaFile(data)); + if (data == null) { + pendingRequests.resolveWithFailure(req, createErrorObject(CAPTURE_NO_MEDIA_FILES, "Error: data is null")); + return; + } + + // Create a file object from the uri + JSONObject mediaFile = createMediaFile(data); + if (mediaFile == null) { + pendingRequests.resolveWithFailure(req, createErrorObject(CAPTURE_INTERNAL_ERR, "Error: no mediaFile created from " + data)); + return; + } + + req.results.put(mediaFile); if (req.results.length() >= req.limit) { // Send Uri back to JavaScript for listening to audio @@ -412,8 +405,21 @@ public class Capture extends CordovaPlugin { } public void onImageActivityResult(Request req) { - // Add image to results - req.results.put(createMediaFile(imageUri)); + // Get the uri of the image + Uri data = imageUri; + if (data == null) { + pendingRequests.resolveWithFailure(req, createErrorObject(CAPTURE_NO_MEDIA_FILES, "Error: data is null")); + return; + } + + // Create a file object from the uri + JSONObject mediaFile = createMediaFile(data); + if (mediaFile == null) { + pendingRequests.resolveWithFailure(req, createErrorObject(CAPTURE_INTERNAL_ERR, "Error: no mediaFile created from " + data)); + return; + } + + req.results.put(mediaFile); checkForDuplicateImage(); @@ -427,32 +433,28 @@ public class Capture extends CordovaPlugin { } public void onVideoActivityResult(Request req, Intent intent) { - Uri data = null; - - if (intent != null){ - // Get the uri of the video clip - data = intent.getData(); + // Get the uri of the video clip + Uri data = intent.getData(); + if (data == null) { + pendingRequests.resolveWithFailure(req, createErrorObject(CAPTURE_NO_MEDIA_FILES, "Error: data is null")); + return; } - if( data == null){ - File movie = new File(getTempDirectoryPath(), "Capture.avi"); - data = Uri.fromFile(movie); + // Create a file object from the uri + JSONObject mediaFile = createMediaFile(data); + if (mediaFile == null) { + pendingRequests.resolveWithFailure(req, createErrorObject(CAPTURE_INTERNAL_ERR, "Error: no mediaFile created from " + data)); + return; } - // create a file object from the uri - if(data == null) { - pendingRequests.resolveWithFailure(req, createErrorObject(CAPTURE_NO_MEDIA_FILES, "Error: data is null")); - } - else { - req.results.put(createMediaFile(data)); + req.results.put(mediaFile); - if (req.results.length() >= req.limit) { - // Send Uri back to JavaScript for viewing video - pendingRequests.resolveWithSuccess(req); - } else { - // still need to capture more video clips - captureVideo(req); - } + if (req.results.length() >= req.limit) { + // Send Uri back to JavaScript for viewing video + pendingRequests.resolveWithSuccess(req); + } else { + // still need to capture more video clips + captureVideo(req); } } @@ -465,6 +467,10 @@ public class Capture extends CordovaPlugin { */ private JSONObject createMediaFile(Uri data) { File fp = webView.getResourceApi().mapUriToFile(data); + if (fp == null) { + return null; + } + JSONObject obj = new JSONObject(); Class webViewClass = webView.getClass(); --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@cordova.apache.org For additional commands, e-mail: commits-h...@cordova.apache.org