You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by st...@apache.org on 2013/09/27 00:27:25 UTC

[02/16] git commit: [CB-4763] Use own version of FileHelper.

[CB-4763] Use own version of FileHelper.


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media-capture/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media-capture/commit/84027b26
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media-capture/tree/84027b26
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media-capture/diff/84027b26

Branch: refs/heads/master
Commit: 84027b269d2b0270b12a9d25a7e87598ac716b0d
Parents: f6451d7
Author: Andrew Grieve <ag...@chromium.org>
Authored: Mon Sep 9 15:02:45 2013 -0400
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Mon Sep 9 15:03:01 2013 -0400

----------------------------------------------------------------------
 plugin.xml                  |  1 +
 src/android/Capture.java    | 16 +++++------
 src/android/FileHelper.java | 60 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 69 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-media-capture/blob/84027b26/plugin.xml
----------------------------------------------------------------------
diff --git a/plugin.xml b/plugin.xml
index 3a59e43..7c8f007 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -55,6 +55,7 @@ xmlns:android="http://schemas.android.com/apk/res/android"
         </config-file>
 
         <source-file src="src/android/Capture.java" target-dir="src/org/apache/cordova/mediacapture" />
+        <source-file src="src/android/FileHelper.java" target-dir="src/org/apache/cordova/mediacapture" />
     </platform>
     
     <!-- ios -->

http://git-wip-us.apache.org/repos/asf/cordova-plugin-media-capture/blob/84027b26/src/android/Capture.java
----------------------------------------------------------------------
diff --git a/src/android/Capture.java b/src/android/Capture.java
index 31443be..1f00a82 100644
--- a/src/android/Capture.java
+++ b/src/android/Capture.java
@@ -28,7 +28,6 @@ import org.apache.cordova.CallbackContext;
 import org.apache.cordova.CordovaPlugin;
 import org.apache.cordova.LOG;
 import org.apache.cordova.PluginResult;
-import org.apache.cordova.FileHelper;
 import org.apache.cordova.DirectoryManager;
 import org.json.JSONArray;
 import org.json.JSONException;
@@ -120,6 +119,7 @@ public class Capture extends CordovaPlugin {
      * @return a MediaFileData object
      */
     private JSONObject getFormatData(String filePath, String mimeType) throws JSONException {
+        Uri fileUrl = filePath.startsWith("file:") ? Uri.parse(filePath) : Uri.fromFile(new File(filePath));
         JSONObject obj = new JSONObject();
         // setup defaults
         obj.put("height", 0);
@@ -131,12 +131,12 @@ public class Capture extends CordovaPlugin {
         // If the mimeType isn't set the rest will fail
         // so let's see if we can determine it.
         if (mimeType == null || mimeType.equals("") || "null".equals(mimeType)) {
-            mimeType = FileHelper.getMimeType(filePath, cordova);
+            mimeType = FileHelper.getMimeType(fileUrl, cordova);
         }
         Log.d(LOG_TAG, "Mime type = " + mimeType);
 
         if (mimeType.equals(IMAGE_JPEG) || filePath.endsWith(".jpg")) {
-            obj = getImageData(filePath, obj);
+            obj = getImageData(fileUrl, obj);
         }
         else if (mimeType.endsWith(AUDIO_3GPP)) {
             obj = getAudioVideoData(filePath, obj, false);
@@ -155,10 +155,10 @@ public class Capture extends CordovaPlugin {
      * @return a JSONObject that represents the Media File Data
      * @throws JSONException
      */
-    private JSONObject getImageData(String filePath, JSONObject obj) throws JSONException {
+    private JSONObject getImageData(Uri fileUrl, JSONObject obj) throws JSONException {
         BitmapFactory.Options options = new BitmapFactory.Options();
         options.inJustDecodeBounds = true;
-        BitmapFactory.decodeFile(FileHelper.stripFileProtocol(filePath), options);
+        BitmapFactory.decodeFile(fileUrl.getPath(), options);
         obj.put("height", options.outHeight);
         obj.put("width", options.outWidth);
         return obj;
@@ -357,13 +357,13 @@ public class Capture extends CordovaPlugin {
      * @throws IOException
      */
     private JSONObject createMediaFile(Uri data) {
-        File fp = new File(FileHelper.getRealPath(data, this.cordova));
+        File fp = webView.getResourceApi().mapUriToFile(data);
         JSONObject obj = new JSONObject();
 
         try {
             // File properties
             obj.put("name", fp.getName());
-            obj.put("fullPath", "file://" + fp.getAbsolutePath());
+            obj.put("fullPath", fp.toURI().toString());
             // Because of an issue with MimeTypeMap.getMimeTypeFromExtension() all .3gpp files
             // are reported as video/3gpp. I'm doing this hacky check of the URI to see if it
             // is stored in the audio or video content store.
@@ -374,7 +374,7 @@ public class Capture extends CordovaPlugin {
                     obj.put("type", VIDEO_3GPP);
                 }
             } else {
-                obj.put("type", FileHelper.getMimeType(fp.getAbsolutePath(), cordova));
+                obj.put("type", FileHelper.getMimeType(Uri.fromFile(fp), cordova));
             }
 
             obj.put("lastModifiedDate", fp.lastModified());

http://git-wip-us.apache.org/repos/asf/cordova-plugin-media-capture/blob/84027b26/src/android/FileHelper.java
----------------------------------------------------------------------
diff --git a/src/android/FileHelper.java b/src/android/FileHelper.java
new file mode 100644
index 0000000..267ad53
--- /dev/null
+++ b/src/android/FileHelper.java
@@ -0,0 +1,60 @@
+/*
+       Licensed to the Apache Software Foundation (ASF) under one
+       or more contributor license agreements.  See the NOTICE file
+       distributed with this work for additional information
+       regarding copyright ownership.  The ASF licenses this file
+       to you under the Apache License, Version 2.0 (the
+       "License"); you may not use this file except in compliance
+       with the License.  You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+       Unless required by applicable law or agreed to in writing,
+       software distributed under the License is distributed on an
+       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+       KIND, either express or implied.  See the License for the
+       specific language governing permissions and limitations
+       under the License.
+ */
+package org.apache.cordova.mediacapture;
+
+import android.net.Uri;
+import android.webkit.MimeTypeMap;
+
+import org.apache.cordova.CordovaInterface;
+
+import java.util.Locale;
+
+// TODO: Replace with CordovaResourceApi.getMimeType() post 3.1.
+public class FileHelper {
+    public static String getMimeTypeForExtension(String path) {
+        String extension = path;
+        int lastDot = extension.lastIndexOf('.');
+        if (lastDot != -1) {
+            extension = extension.substring(lastDot + 1);
+        }
+        // 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 MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
+    }
+    
+    /**
+     * Returns the mime type of the data specified by the given URI string.
+     *
+     * @param uriString the URI string of the data
+     * @return the mime type of the specified data
+     */
+    public static String getMimeType(Uri uri, CordovaInterface cordova) {
+        String mimeType = null;
+        if ("content".equals(uri.getScheme())) {
+            mimeType = cordova.getActivity().getContentResolver().getType(uri);
+        } else {
+            mimeType = getMimeTypeForExtension(uri.getPath());
+        }
+
+        return mimeType;
+    }
+}