CB-11625: Working on fix to API 24 no longer allowing File URIs to be passed across intents

This commit is contained in:
Joe Bowser 2016-07-27 14:06:07 -07:00
parent b695717240
commit 3d26986bfd
2 changed files with 126 additions and 88 deletions

View File

@ -69,6 +69,17 @@
<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</config-file>
<config file target="AndroidManifest.xml" parent="application">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</config>
<source-file src="src/android/CameraLauncher.java" target-dir="src/org/apache/cordova/camera" />
<source-file src="src/android/FileHelper.java" target-dir="src/org/apache/cordova/camera" />
@ -78,6 +89,8 @@
<clobbers target="CameraPopoverHandle" />
</js-module>
<framework src="com.android.support:support-v4:24.1.1+" />
</platform>
<!-- amazon-fireos -->

View File

@ -36,6 +36,7 @@ import org.apache.cordova.CordovaResourceApi;
import org.apache.cordova.LOG;
import org.apache.cordova.PermissionHelper;
import org.apache.cordova.PluginResult;
import org.apache.mobilespec.BuildConfig;
import org.json.JSONArray;
import org.json.JSONException;
@ -56,6 +57,8 @@ import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.support.v4.content.FileProvider;
import android.util.Base64;
import android.util.Log;
import android.content.pm.PackageManager;
@ -281,8 +284,12 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
// Specify file so that large image is captured and returned
File photo = createCaptureFile(encodingType);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
this.imageUri = Uri.fromFile(photo);
this.imageUri = FileProvider.getUriForFile(cordova.getActivity(),
BuildConfig.APPLICATION_ID + ".provider",
photo);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, this.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);
if (this.cordova != null) {
// Let's check to make sure the camera is actually installed. (Legacy Nexus 7 code)
@ -416,7 +423,9 @@ 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() + ""));
croppedUri = FileProvider.getUriForFile(cordova.getActivity(),
BuildConfig.APPLICATION_ID + ".provider",
createCaptureFile(this.encodingType, System.currentTimeMillis() + ""));
cropIntent.putExtra("output", croppedUri);
// start the activity - we handle returning in onActivityResult
@ -449,9 +458,11 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
// Create an ExifHelper to save the exif data that is lost during compression
ExifHelper exif = new ExifHelper();
String sourcePath = (this.allowEdit && this.croppedUri != null) ?
FileHelper.stripFileProtocol(this.croppedUri.toString()) :
FileHelper.stripFileProtocol(this.imageUri.toString());
getFileNameFromUri(this.croppedUri) :
getFileNameFromUri(this.imageUri);
if (this.encodingType == JPEG) {
try {
@ -472,12 +483,15 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
// in the gallery and the modified image is saved in the temporary
// directory
if (this.saveToPhotoAlbum) {
galleryUri = Uri.fromFile(new File(getPicutresPath()));
galleryUri = Uri.fromFile(new File(getPicturesPath()));
if (this.allowEdit && this.croppedUri != null) {
writeUncompressedImage(this.croppedUri, galleryUri);
Uri croppedUri = Uri.fromFile(new File(getFileNameFromUri(this.croppedUri)));
writeUncompressedImage(croppedUri, galleryUri);
} else {
writeUncompressedImage(this.imageUri, galleryUri);
Uri imageUri = Uri.fromFile(new File(getFileNameFromUri(this.imageUri)));
writeUncompressedImage(imageUri, galleryUri);
}
refreshGallery(galleryUri);
@ -524,9 +538,11 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
Uri uri = Uri.fromFile(createCaptureFile(this.encodingType, System.currentTimeMillis() + ""));
if (this.allowEdit && this.croppedUri != null) {
writeUncompressedImage(this.croppedUri, uri);
Uri croppedUri = Uri.fromFile(new File(getFileNameFromUri(this.croppedUri)));
writeUncompressedImage(croppedUri, uri);
} else {
writeUncompressedImage(this.imageUri, uri);
Uri imageUri = Uri.fromFile(new File(getFileNameFromUri(this.imageUri)));
writeUncompressedImage(imageUri, uri);
}
this.callbackContext.success(uri.toString());
@ -575,8 +591,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
bitmap = null;
}
private String getPicutresPath()
{
private String getPicturesPath() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "IMG_" + timeStamp + (this.encodingType == JPEG ? ".jpg" : ".png");
File storageDir = Environment.getExternalStoragePublicDirectory(
@ -585,8 +600,7 @@ private String getPicutresPath()
return galleryPath;
}
private void refreshGallery(Uri contentUri)
{
private void refreshGallery(Uri contentUri) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(contentUri);
this.cordova.getActivity().sendBroadcast(mediaScanIntent);
@ -632,7 +646,6 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
}
/**
* Applies all needed transformation to the image received from the gallery.
*
@ -658,8 +671,7 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
// and there will be no attempt to resize any returned data
if (this.mediaType != PICTURE) {
this.callbackContext.success(fileLocation);
}
else {
} else {
// This is a special case to just return the path as no scaling,
// rotating, nor compressing needs to be done
if (this.targetHeight == -1 && this.targetWidth == -1 &&
@ -721,8 +733,7 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
e.printStackTrace();
this.failPicture("Error retrieving image.");
}
}
else {
} else {
this.callbackContext.success(fileLocation);
}
}
@ -778,12 +789,12 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
// If image available
if (resultCode == Activity.RESULT_OK) {
try {
if(this.allowEdit)
{
Uri tmpFile = Uri.fromFile(createCaptureFile(this.encodingType));
if (this.allowEdit) {
Uri tmpFile = FileProvider.getUriForFile(cordova.getActivity(),
BuildConfig.APPLICATION_ID + ".provider",
createCaptureFile(this.encodingType));
performCrop(tmpFile, destType, intent);
}
else {
} else {
this.processResultFromCamera(destType, intent);
}
} catch (IOException e) {
@ -812,11 +823,9 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
processResultFromGallery(finalDestType, i);
}
});
}
else if (resultCode == Activity.RESULT_CANCELED) {
} else if (resultCode == Activity.RESULT_CANCELED) {
this.failPicture("Selection cancelled.");
}
else {
} else {
this.failPicture("Selection did not complete!");
}
}
@ -855,13 +864,10 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
matrix.setRotate(rotate, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
}
try
{
try {
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
exif.resetOrientation();
}
catch (OutOfMemoryError oom)
{
} catch (OutOfMemoryError oom) {
// You can run out of memory if the image is very large:
// http://simonmacdonald.blogspot.ca/2012/07/change-to-camera-code-in-phonegap-190.html
// If this happens, simply do not rotate the image and return it unmodified.
@ -978,8 +984,7 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
}
//CB-2292: WTF? Why is the width null?
if(options.outWidth == 0 || options.outHeight == 0)
{
if (options.outWidth == 0 || options.outHeight == 0) {
return null;
}
@ -1093,6 +1098,7 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
/**
* Cleans up after picture taking. Checking for duplicates and that kind of stuff.
*
* @param newImage
*/
private void cleanup(int imageType, Uri oldImage, Uri newImage, Bitmap bitmap) {
@ -1144,6 +1150,7 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
/**
* Determine if we are storing the images in internal or external storage
*
* @return Uri
*/
private Uri whichContentStore() {
@ -1214,18 +1221,14 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
public void onRequestPermissionResult(int requestCode, String[] permissions,
int[] grantResults) throws JSONException
{
for(int r:grantResults)
{
if(r == PackageManager.PERMISSION_DENIED)
{
int[] grantResults) throws JSONException {
for (int r : grantResults) {
if (r == PackageManager.PERMISSION_DENIED) {
this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR));
return;
}
}
switch(requestCode)
{
switch (requestCode) {
case TAKE_PIC_SEC:
takePicture(this.destType, this.encodingType);
break;
@ -1288,4 +1291,26 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
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;
}
}