diff --git a/plugin.xml b/plugin.xml index 1455e80a..384abae7 100644 --- a/plugin.xml +++ b/plugin.xml @@ -76,6 +76,18 @@ xmlns:android="http://schemas.android.com/apk/res/android" + + + + + + + + + + + + diff --git a/src/android/Capture.java b/src/android/Capture.java index 43bcc57f..b6a3365b 100644 --- a/src/android/Capture.java +++ b/src/android/Capture.java @@ -18,8 +18,13 @@ Licensed to the Apache Software Foundation (ASF) under one */ package org.apache.cordova.mediacapture; +import static org.apache.cordova.mediacapture.FileHelper.AUDIO_3GPP; +import static org.apache.cordova.mediacapture.FileHelper.AUDIO_MP4; + import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -59,8 +64,7 @@ public class Capture extends CordovaPlugin { private static final String VIDEO_3GPP = "video/3gpp"; private static final String VIDEO_MP4 = "video/mp4"; - private static final String AUDIO_3GPP = "audio/3gpp"; - private static final String[] AUDIO_TYPES = new String[] {"audio/3gpp", "audio/aac", "audio/amr", "audio/wav"}; + private static final String[] AUDIO_TYPES = new String[] {AUDIO_3GPP, "audio/aac", "audio/amr", "audio/wav", AUDIO_MP4}; private static final String IMAGE_JPEG = "image/jpeg"; private static final int CAPTURE_AUDIO = 0; // Constant for capture audio @@ -377,19 +381,60 @@ 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) { + Uri srcContentUri = intent.getData(); + + if (srcContentUri == 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)); + // 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 create new file to application cache directory.")); + return; + } + } catch (IOException e) { + pendingRequests.resolveWithFailure(req, createErrorObject(CAPTURE_NO_MEDIA_FILES, "Error: failed to copy recording to application cache directory.")); + 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) { + pendingRequests.resolveWithFailure(req, createErrorObject(CAPTURE_INTERNAL_ERR, "Error: no mediaFile created from " + srcContentUri)); return; } diff --git a/src/android/FileHelper.java b/src/android/FileHelper.java index 267ad530..408c21f2 100644 --- a/src/android/FileHelper.java +++ b/src/android/FileHelper.java @@ -27,6 +27,9 @@ Licensed to the Apache Software Foundation (ASF) under one // TODO: Replace with CordovaResourceApi.getMimeType() post 3.1. public class FileHelper { + static final String AUDIO_3GPP = "audio/3gpp"; + static final String AUDIO_MP4 = "audio/mp4"; + public static String getMimeTypeForExtension(String path) { String extension = path; int lastDot = extension.lastIndexOf('.'); @@ -36,7 +39,9 @@ public static String getMimeTypeForExtension(String path) { // Convert the URI string to lower case to ensure compatibility with MimeTypeMap (see CB-2185). extension = extension.toLowerCase(Locale.getDefault()); if (extension.equals("3ga")) { - return "audio/3gpp"; + return AUDIO_3GPP; + } else if (extension.equals("m4a")) { + return AUDIO_MP4; } return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); }