Skip to content

Commit

Permalink
fix(android): prevent NPE with null checks on intent data and mediaFile
Browse files Browse the repository at this point in the history
  • Loading branch information
ath0mas committed Nov 6, 2021
1 parent 1136b8e commit 8313b55
Showing 1 changed file with 49 additions and 16 deletions.
65 changes: 49 additions & 16 deletions src/android/Capture.java
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,19 @@ else if (resultCode == Activity.RESULT_CANCELED) {
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
Expand All @@ -379,8 +390,21 @@ public void onAudioActivityResult(Request req, Intent intent) {
}

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();

Expand All @@ -396,21 +420,26 @@ public void onImageActivityResult(Request req) {
public void onVideoActivityResult(Request req, Intent intent) {
// Get the uri of the video clip
Uri data = intent.getData();

// create a file object from the uri
if(data == null) {
if (data == null) {
pendingRequests.resolveWithFailure(req, createErrorObject(CAPTURE_NO_MEDIA_FILES, "Error: data is null"));
return;
}
else {
req.results.put(createMediaFile(data));

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);
}
// 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 viewing video
pendingRequests.resolveWithSuccess(req);
} else {
// still need to capture more video clips
captureVideo(req);
}
}

Expand All @@ -423,6 +452,10 @@ public void onVideoActivityResult(Request req, Intent intent) {
*/
private JSONObject createMediaFile(Uri data) {
File fp = webView.getResourceApi().mapUriToFile(data);
if (fp == null) {
return null;
}

JSONObject obj = new JSONObject();

Class webViewClass = webView.getClass();
Expand Down

0 comments on commit 8313b55

Please sign in to comment.