erisu commented on code in PR #277:
URL: 
https://github.com/apache/cordova-plugin-media-capture/pull/277#discussion_r1286485532


##########
src/android/Capture.java:
##########
@@ -377,20 +381,54 @@ else if (resultCode == Activity.RESULT_CANCELED) {
         }
     }
 
-
     public void onAudioActivityResult(Request req, Intent intent) {
-        // Get the uri of the audio clip
-        Uri data = intent.getData();
-        if (data == null) {
-            pendingRequests.resolveWithFailure(req, 
createErrorObject(CAPTURE_NO_MEDIA_FILES, "Error: data is null"));
-            return;
+        Uri srcContentUri = intent.getData();
+
+        // Get file name
+        String srcContentUriString = srcContentUri.toString();
+        String fileName = 
srcContentUriString.substring(srcContentUriString.lastIndexOf('/') + 1);
+
+        // Create dest file path
+        String tempRoot = 
cordova.getActivity().getCacheDir().getAbsolutePath();
+        String destFile = tempRoot + "/" + fileName;
+
+        // Create dest file
+        File tmpRootFile = new File(destFile);
+
+        try {
+            // If file exists

Review Comment:
   I can split the try-catch like this.
   
   ```java
   // Create dest file
   File tmpRootFile = new File(destFile);
   
   try {
       if (!tmpRootFile.createNewFile()) {
           // When the file already exists
           pendingRequests.resolveWithFailure(req, 
createErrorObject(CAPTURE_NO_MEDIA_FILES, "Error: failed to create file to 
application cache directory as it already exists."));
           return;
       }
   } catch (IOException e) {
       // When the path is invalid
       pendingRequests.resolveWithFailure(req, 
createErrorObject(CAPTURE_NO_MEDIA_FILES, "Error: failed to create new file to 
application cache directory due to invalid file path."));
       return;
   }
   
   try {
       // Write src file content to dest (new file)
   } catch (IOException e) {
       // When it fails to copy content over
       pendingRequests.resolveWithFailure(req, 
createErrorObject(CAPTURE_NO_MEDIA_FILES, "Error: failed to copy recording data 
to new file in application cache directory."));
       return;
   }
   ```
   
   This will provide better feedback on what fails.
   
   - When the file already exists
   - When the path is invalid and could not create a file
   - When it fails to copy the src file contents to the dest file (new file).
   
   Alternatively, we use `createTempFile` and do not reuse the filename from 
the recorder. There is always a chance that the UUID filename that the recorder 
app created is not unique to the app. 
   
   ```java
   // Create dest file
   String fileExtension = "." + 
srcContentUriString.substring(srcContentUriString.lastIndexOf('.') + 1);
   File tempRootFile = new File(tempRoot);
   File tmpNewFile = null;
   
   try {
       tmpNewFile = File.createTempFile("cdv-media-capture", fileExtension, 
tempRootFile);
   }  catch (IOException e) {
       pendingRequests.resolveWithFailure(req, 
createErrorObject(CAPTURE_NO_MEDIA_FILES, "Error: failed to create new temp 
file in application cache directory."));
       return;
   }
   
   try {
       // Write src file content to dest (new file)
   } catch (IOException e) {
       // When it fails to copy content over
       pendingRequests.resolveWithFailure(req, 
createErrorObject(CAPTURE_NO_MEDIA_FILES, "Error: failed to copy recording data 
to new file in application cache directory."));
       return;
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to