Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

breaking(android): stop using CordovaUri helper class #617

Merged
merged 2 commits into from Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion plugin.xml
Expand Up @@ -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" />
Expand Down
57 changes: 22 additions & 35 deletions src/android/CameraLauncher.java
Expand Up @@ -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)
Expand All @@ -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;

Expand Down Expand Up @@ -290,10 +292,11 @@ public void takePicture(int returnType, int encodingType)

// 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);

Expand Down Expand Up @@ -360,6 +363,7 @@ public void getImage(int srcType, int returnType, int encodingType) {
Intent intent = new Intent();
String title = GET_PICTURE;
croppedUri = null;
croppedFilePath = null;
if (this.mediaType == PICTURE) {
intent.setType("image/*");
if (this.allowEdit) {
Expand All @@ -375,8 +379,9 @@ public void getImage(int srcType, int returnType, int encodingType) {
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);
Expand Down Expand Up @@ -428,7 +433,8 @@ private void performCrop(Uri picUri, int destType, Intent cameraIntent) {
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);
Expand Down Expand Up @@ -466,8 +472,8 @@ private void processResultFromCamera(int destType, Intent intent) throws IOExcep
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) {
Expand All @@ -494,7 +500,7 @@ private void processResultFromCamera(int destType, Intent intent) throws IOExcep
if (this.allowEdit && this.croppedUri != null) {
writeUncompressedImage(croppedUri, galleryUri);
} else {
Uri imageUri = this.imageUri.getFileUri();
Uri imageUri = this.imageUri;
writeUncompressedImage(imageUri, galleryUri);
}

Expand Down Expand Up @@ -539,10 +545,10 @@ else if (destType == FILE_URI || destType == NATIVE_URI) {
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);
}

Expand Down Expand Up @@ -588,7 +594,7 @@ else if (destType == FILE_URI || destType == NATIVE_URI) {
throw new IllegalStateException();
}

this.cleanup(FILE_URI, this.imageUri.getFileUri(), galleryUri, bitmap);
this.cleanup(FILE_URI, this.imageUri, galleryUri, bitmap);
bitmap = null;
}

Expand Down Expand Up @@ -1336,11 +1342,11 @@ public Bundle onSaveInstanceState() {
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;
Expand All @@ -1365,28 +1371,9 @@ public void onRestoreStateForActivityResult(Bundle state, CallbackContext callba

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;
}
}
104 changes: 0 additions & 104 deletions src/android/CordovaUri.java

This file was deleted.

17 changes: 3 additions & 14 deletions src/android/FileHelper.java
Expand Up @@ -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);
}

/**
Expand All @@ -75,11 +66,9 @@ public static String getRealPath(String uriString, CordovaInterface cordova) {
}

@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)) {
Expand Down
2 changes: 1 addition & 1 deletion src/android/xml/camera_provider_paths.xml
Expand Up @@ -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>