You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by jc...@apache.org on 2020/07/14 14:26:23 UTC

[cordova-plugin-camera] branch master updated: breaking(android): stop using CordovaUri helper class (#617)

This is an automated email from the ASF dual-hosted git repository.

jcesarmobile pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-plugin-camera.git


The following commit(s) were added to refs/heads/master by this push:
     new fd155d9  breaking(android): stop using CordovaUri helper class (#617)
fd155d9 is described below

commit fd155d9705b7de4b14737b27d942d9ecd4ef2eac
Author: jcesarmobile <jc...@gmail.com>
AuthorDate: Tue Jul 14 16:26:09 2020 +0200

    breaking(android): stop using CordovaUri helper class (#617)
---
 plugin.xml                                |   1 -
 src/android/CameraLauncher.java           |  57 +++++++---------
 src/android/CordovaUri.java               | 104 ------------------------------
 src/android/FileHelper.java               |  17 +----
 src/android/xml/camera_provider_paths.xml |   2 +-
 5 files changed, 26 insertions(+), 155 deletions(-)

diff --git a/plugin.xml b/plugin.xml
index 5716ecb..703f9d8 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -69,7 +69,6 @@
         </config-file>
 
         <source-file src="src/android/CameraLauncher.java" target-dir="src/org/apache/cordova/camera" />
-        <source-file src="src/android/CordovaUri.java" target-dir="src/org/apache/cordova/camera" />
         <source-file src="src/android/FileHelper.java" target-dir="src/org/apache/cordova/camera" />
         <source-file src="src/android/ExifHelper.java" target-dir="src/org/apache/cordova/camera" />
         <source-file src="src/android/FileProvider.java" target-dir="src/org/apache/cordova/camera" />
diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java
index 801489d..d28aaa5 100644
--- a/src/android/CameraLauncher.java
+++ b/src/android/CameraLauncher.java
@@ -109,7 +109,8 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
     private int mQuality;                   // Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality)
     private int targetWidth;                // desired width of the image
     private int targetHeight;               // desired height of the image
-    private CordovaUri imageUri;            // Uri of captured image
+    private Uri imageUri;                   // Uri of captured image
+    private String imageFilePath;           // File where the image is stored
     private int encodingType;               // Type of encoding to use
     private int mediaType;                  // What type of media to retrieve
     private int destType;                   // Source type (needs to be saved for the permission handling)
@@ -127,6 +128,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
     private MediaScannerConnection conn;    // Used to update gallery app with newly-written files
     private Uri scanMe;                     // Uri of image to be added to content store
     private Uri croppedUri;
+    private String croppedFilePath;
     private ExifHelper exifData;            // Exif data from source
     private String applicationId;
 
@@ -290,10 +292,11 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
 
         // Specify file so that large image is captured and returned
         File photo = createCaptureFile(encodingType);
-        this.imageUri = new CordovaUri(FileProvider.getUriForFile(cordova.getActivity(),
+        this.imageFilePath = photo.getAbsolutePath();
+        this.imageUri = FileProvider.getUriForFile(cordova.getActivity(),
                 applicationId + ".provider",
-                photo));
-        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri.getCorrectUri());
+                photo);
+        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
         //We can write to this URI, this will hopefully allow us to write files to get to the next step
         intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
 
@@ -360,6 +363,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
         Intent intent = new Intent();
         String title = GET_PICTURE;
         croppedUri = null;
+        croppedFilePath = null;
         if (this.mediaType == PICTURE) {
             intent.setType("image/*");
             if (this.allowEdit) {
@@ -375,8 +379,9 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
                     intent.putExtra("aspectX", 1);
                     intent.putExtra("aspectY", 1);
                 }
-                File photo = createCaptureFile(JPEG);
-                croppedUri = Uri.fromFile(photo);
+                File croppedFile = createCaptureFile(JPEG);
+                croppedFilePath = croppedFile.getAbsolutePath();
+                croppedUri = Uri.fromFile(croppedFile);
                 intent.putExtra(MediaStore.EXTRA_OUTPUT, croppedUri);
             } else {
                 intent.setAction(Intent.ACTION_GET_CONTENT);
@@ -428,7 +433,8 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
           cropIntent.putExtra("aspectY", 1);
         }
         // create new file handle to get full resolution crop
-        croppedUri = Uri.fromFile(createCaptureFile(this.encodingType, System.currentTimeMillis() + ""));
+        croppedFilePath = createCaptureFile(this.encodingType, System.currentTimeMillis() + "").getAbsolutePath();
+        croppedUri = Uri.parse(croppedFilePath);
         cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
         cropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
         cropIntent.putExtra("output", croppedUri);
@@ -466,8 +472,8 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
         ExifHelper exif = new ExifHelper();
 
         String sourcePath = (this.allowEdit && this.croppedUri != null) ?
-                FileHelper.stripFileProtocol(this.croppedUri.toString()) :
-                this.imageUri.getFilePath();
+                this.croppedFilePath :
+                this.imageFilePath;
 
 
         if (this.encodingType == JPEG) {
@@ -494,7 +500,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
             if (this.allowEdit && this.croppedUri != null) {
                 writeUncompressedImage(croppedUri, galleryUri);
             } else {
-                Uri imageUri = this.imageUri.getFileUri();
+                Uri imageUri = this.imageUri;
                 writeUncompressedImage(imageUri, galleryUri);
             }
 
@@ -539,10 +545,10 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
                     Uri uri = Uri.fromFile(createCaptureFile(this.encodingType, System.currentTimeMillis() + ""));
 
                     if (this.allowEdit && this.croppedUri != null) {
-                        Uri croppedUri = Uri.fromFile(new File(getFileNameFromUri(this.croppedUri)));
+                        Uri croppedUri = Uri.parse(croppedFilePath);
                         writeUncompressedImage(croppedUri, uri);
                     } else {
-                        Uri imageUri = this.imageUri.getFileUri();
+                        Uri imageUri = this.imageUri;
                         writeUncompressedImage(imageUri, uri);
                     }
 
@@ -588,7 +594,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
             throw new IllegalStateException();
         }
 
-        this.cleanup(FILE_URI, this.imageUri.getFileUri(), galleryUri, bitmap);
+        this.cleanup(FILE_URI, this.imageUri, galleryUri, bitmap);
         bitmap = null;
     }
 
@@ -1336,11 +1342,11 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
         state.putBoolean("saveToPhotoAlbum", this.saveToPhotoAlbum);
 
         if (this.croppedUri != null) {
-            state.putString(CROPPED_URI_KEY, this.croppedUri.toString());
+            state.putString(CROPPED_URI_KEY, this.croppedFilePath);
         }
 
         if (this.imageUri != null) {
-            state.putString(IMAGE_URI_KEY, this.imageUri.getFileUri().toString());
+            state.putString(IMAGE_URI_KEY, this.imageFilePath);
         }
 
         return state;
@@ -1365,28 +1371,9 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
 
         if (state.containsKey(IMAGE_URI_KEY)) {
             //I have no idea what type of URI is being passed in
-            this.imageUri = new CordovaUri(Uri.parse(state.getString(IMAGE_URI_KEY)));
+            this.imageUri = Uri.parse(state.getString(IMAGE_URI_KEY));
         }
 
         this.callbackContext = callbackContext;
     }
-
-     /*
-      * This is dirty, but it does the job.
-      *
-      * Since the FilesProvider doesn't really provide you a way of getting a URL from the file,
-      * and since we actually need the Camera to create the file for us most of the time, we don't
-      * actually write the file, just generate the location based on a timestamp, we need to get it
-      * back from the Intent.
-      *
-      * However, the FilesProvider preserves the path, so we can at least write to it from here, since
-      * we own the context in this case.
-     */
-    private String getFileNameFromUri(Uri uri) {
-        String fullUri = uri.toString();
-        String partial_path = fullUri.split("external_files")[1];
-        File external_storage = Environment.getExternalStorageDirectory();
-        String path = external_storage.getAbsolutePath() + partial_path;
-        return path;
-    }
 }
diff --git a/src/android/CordovaUri.java b/src/android/CordovaUri.java
deleted file mode 100644
index 5c2224d..0000000
--- a/src/android/CordovaUri.java
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
-       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.camera;
-
-import android.net.Uri;
-import android.os.Build;
-import android.os.Environment;
-import android.support.v4.content.FileProvider;
-
-import java.io.File;
-
-/*
- * This class exists because Andorid FilesProvider doesn't work on Android 4.4.4 and below and throws
- * weird errors.  I'm not sure why writing to shared cache directories is somehow verboten, but it is
- * and this error is irritating for a Compatibility library to have.
- *
- */
-
-public class CordovaUri {
-
-    private Uri androidUri;
-    private String fileName;
-    private Uri fileUri;
-
-    /*
-     * We always expect a FileProvider string to be passed in for the file that we create
-     *
-     */
-    CordovaUri (Uri inputUri)
-    {
-        //Determine whether the file is a content or file URI
-        if(inputUri.getScheme().equals("content"))
-        {
-            androidUri = inputUri;
-            fileName = getFileNameFromUri(androidUri);
-            fileUri = Uri.parse("file://" + fileName);
-        }
-        else
-        {
-            fileUri = inputUri;
-            fileName = FileHelper.stripFileProtocol(inputUri.toString());
-        }
-    }
-
-    public Uri getFileUri()
-    {
-        return fileUri;
-    }
-
-    public String getFilePath()
-    {
-        return fileName;
-    }
-
-    /*
-     * This only gets called by takePicture
-     */
-
-    public Uri getCorrectUri()
-    {
-        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
-            return androidUri;
-        else
-            return fileUri;
-    }
-
- /*
-  * This is dirty, but it does the job.
-  *
-  * Since the FilesProvider doesn't really provide you a way of getting a URL from the file,
-  * and since we actually need the Camera to create the file for us most of the time, we don't
-  * actually write the file, just generate the location based on a timestamp, we need to get it
-  * back from the Intent.
-  *
-  * However, the FilesProvider preserves the path, so we can at least write to it from here, since
-  * we own the context in this case.
- */
-
-    private String getFileNameFromUri(Uri uri) {
-        String fullUri = uri.toString();
-        String partial_path = fullUri.split("external_files")[1];
-        File external_storage = Environment.getExternalStorageDirectory();
-        String path = external_storage.getAbsolutePath() + partial_path;
-        return path;
-
-    }
-}
diff --git a/src/android/FileHelper.java b/src/android/FileHelper.java
index edf1657..7a5524b 100644
--- a/src/android/FileHelper.java
+++ b/src/android/FileHelper.java
@@ -50,16 +50,7 @@ public class FileHelper {
      */
     @SuppressWarnings("deprecation")
     public static String getRealPath(Uri uri, CordovaInterface cordova) {
-        String realPath = null;
-
-        if (Build.VERSION.SDK_INT < 11)
-            realPath = FileHelper.getRealPathFromURI_BelowAPI11(cordova.getActivity(), uri);
-
-        // SDK >= 11
-        else
-            realPath = FileHelper.getRealPathFromURI_API11_And_Above(cordova.getActivity(), uri);
-
-        return realPath;
+        return FileHelper.getRealPathFromURI(cordova.getActivity(), uri);
     }
 
     /**
@@ -75,11 +66,9 @@ public class FileHelper {
     }
 
     @SuppressLint("NewApi")
-    public static String getRealPathFromURI_API11_And_Above(final Context context, final Uri uri) {
-
-        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
+    public static String getRealPathFromURI(final Context context, final Uri uri) {
         // DocumentProvider
-        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
+        if (DocumentsContract.isDocumentUri(context, uri)) {
 
             // ExternalStorageProvider
             if (isExternalStorageDocument(uri)) {
diff --git a/src/android/xml/camera_provider_paths.xml b/src/android/xml/camera_provider_paths.xml
index 2db87bd..3266748 100644
--- a/src/android/xml/camera_provider_paths.xml
+++ b/src/android/xml/camera_provider_paths.xml
@@ -17,5 +17,5 @@
 -->
 
 <paths xmlns:android="http://schemas.android.com/apk/res/android">
-    <external-path name="external_files" path="."/>
+    <cache-path name="cache_files" path="." />
 </paths>
\ No newline at end of file


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