You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cordova.apache.org by "ath0mas (via GitHub)" <gi...@apache.org> on 2023/08/09 17:05:41 UTC

[GitHub] [cordova-plugin-media-capture] ath0mas commented on a diff in pull request #277: [PoC] fix(android): API 30+ capture audio

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


##########
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
+            if (tmpRootFile.createNewFile()) {
+                FileOutputStream destFOS = new FileOutputStream(tmpRootFile);
+
+                byte[] buf = new byte[8192];
+                int length;
+
+                ContentResolver srcContentResolver = cordova.getContext().getContentResolver();
+                InputStream srcIS = srcContentResolver.openInputStream(srcContentUri);
+
+                while ((length = srcIS.read(buf)) != -1) {
+                    destFOS.write(buf, 0, length);
+                }
+            } else {
+                pendingRequests.resolveWithFailure(req, createErrorObject(CAPTURE_NO_MEDIA_FILES, "Error: failed to copy recording to application cache directory."));
+                return;
+            }
+        } catch (IOException e) {
+            throw new RuntimeException(e);
         }
 
-        // 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;
+        LOG.d(LOG_TAG, "Recording file path: " + destFile);
+
+        JSONObject mediaFile = new JSONObject();
+        try {
+            // File properties
+            mediaFile.put("name", tmpRootFile.getName());
+            mediaFile.put("fullPath", destFile);
+            mediaFile.put("type", FileHelper.getMimeType(Uri.fromFile(tmpRootFile), cordova));
+            mediaFile.put("lastModifiedDate", tmpRootFile.lastModified());
+            mediaFile.put("size", tmpRootFile.length());
+        } catch (JSONException e) {
+            throw new RuntimeException(e);

Review Comment:
   See `// this will never happen` for this same exception, and maybe prevent duplicate code extracting and calling a `createMediaFile(File)`.



-- 
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: issues-unsubscribe@cordova.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org