Skip to content

Commit 3cbb139

Browse files
committed
TMA-1630: Camera plugin updates for playstore requirements
1 parent eb69db0 commit 3cbb139

File tree

3 files changed

+28
-35
lines changed

3 files changed

+28
-35
lines changed

plugin.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@
5656
</config-file>
5757
<config-file target="AndroidManifest.xml" parent="/*">
5858
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
59-
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
60-
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
6159
</config-file>
6260
<config-file target="AndroidManifest.xml" parent="application">
6361
<provider

src/android/CameraLauncher.java

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Licensed to the Apache Software Foundation (ASF) under one
6161
import java.io.OutputStream;
6262
import java.text.SimpleDateFormat;
6363
import java.util.Date;
64+
import java.util.ArrayList;
6465

6566
/**
6667
* This class launches the camera view, allows the user to take a picture, closes the camera view,
@@ -227,34 +228,19 @@ else if ((this.srcType == PHOTOLIBRARY) || (this.srcType == SAVEDPHOTOALBUM)) {
227228
//--------------------------------------------------------------------------
228229

229230
private String[] getPermissions(boolean storageOnly, int mediaType) {
230-
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
231-
if (storageOnly) {
232-
switch (mediaType) {
233-
case PICTURE:
234-
return new String[]{ Manifest.permission.READ_MEDIA_IMAGES };
235-
case VIDEO:
236-
return new String[]{ Manifest.permission.READ_MEDIA_VIDEO };
237-
default:
238-
return new String[]{ Manifest.permission.READ_MEDIA_IMAGES, Manifest.permission.READ_MEDIA_VIDEO };
239-
}
240-
}
241-
else {
242-
switch (mediaType) {
243-
case PICTURE:
244-
return new String[]{ Manifest.permission.CAMERA, Manifest.permission.CAMERA, Manifest.permission.READ_MEDIA_IMAGES };
245-
case VIDEO:
246-
return new String[]{ Manifest.permission.CAMERA, Manifest.permission.READ_MEDIA_VIDEO };
247-
default:
248-
return new String[]{ Manifest.permission.CAMERA, Manifest.permission.READ_MEDIA_IMAGES, Manifest.permission.READ_MEDIA_VIDEO };
249-
}
250-
}
251-
} else {
252-
if (storageOnly) {
253-
return new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
254-
} else {
255-
return new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
256-
}
231+
ArrayList<String> permissions = new ArrayList<>();
232+
233+
if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
234+
// Android API 30 or lower
235+
permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
236+
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
257237
}
238+
if (!storageOnly) {
239+
// Add camera permission when not storage.
240+
permissions.add(Manifest.permission.CAMERA);
241+
}
242+
243+
return permissions.toArray(new String[0]);
258244
}
259245

260246
private String getTempDirectoryPath() {
@@ -381,7 +367,10 @@ private File createCaptureFile(int encodingType, String fileName) {
381367
throw new IllegalArgumentException("Invalid Encoding Type: " + encodingType);
382368
}
383369

384-
return new File(getTempDirectoryPath(), fileName);
370+
File cacheDir = new File(getTempDirectoryPath(), "org.apache.cordova.camera");
371+
cacheDir.mkdir();
372+
373+
return new File(cacheDir, fileName);
385374
}
386375

387376

@@ -529,7 +518,7 @@ private void processResultFromCamera(int destType, Intent intent) throws IOExcep
529518
if (this.allowEdit && this.croppedUri != null) {
530519
writeUncompressedImage(croppedUri, galleryUri);
531520
} else {
532-
if (Build.VERSION.SDK_INT <= 28) { // Between LOLLIPOP_MR1 and P, can be changed later to the constant Build.VERSION_CODES.P
521+
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
533522
writeTakenPictureToGalleryLowerThanAndroidQ(galleryUri);
534523
} else { // Android Q or higher
535524
writeTakenPictureToGalleryStartingFromAndroidQ(galleryPathVO);
@@ -752,7 +741,13 @@ private void processResultFromGallery(int destType, Intent intent) {
752741
// If you ask for video or the selected file cannot be processed
753742
// there will be no attempt to resize any returned data.
754743
if (this.mediaType == VIDEO || !isImageMimeTypeProcessable(mimeTypeOfGalleryFile)) {
755-
this.callbackContext.success(finalLocation);
744+
745+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
746+
Uri tempUri = copyFromUriToTempFile(uri);
747+
this.callbackContext.success(tempUri.toString());
748+
} else {
749+
this.callbackContext.success(finalLocation);
750+
}
756751
} else {
757752

758753
// This is a special case to just return the path as no scaling,
@@ -1022,7 +1017,7 @@ private Bitmap getScaledAndRotatedBitmap(String imageUrl) throws IOException {
10221017
// Generate a temporary file
10231018
String timeStamp = new SimpleDateFormat(TIME_FORMAT).format(new Date());
10241019
String fileName = "IMG_" + timeStamp + (getExtensionForEncodingType());
1025-
localFile = new File(getTempDirectoryPath() + fileName);
1020+
localFile = new File(getTempDirectoryPath(), fileName);
10261021
galleryUri = Uri.fromFile(localFile);
10271022
writeUncompressedImage(fileStream, galleryUri);
10281023
try {
@@ -1408,7 +1403,7 @@ public Bundle onSaveInstanceState() {
14081403
}
14091404

14101405
if (this.imageUri != null) {
1411-
state.putString(IMAGE_URI_KEY, this.imageFilePath);
1406+
state.putString(IMAGE_URI_KEY, this.imageUri.toString());
14121407
}
14131408

14141409
if (this.imageFilePath != null) {

src/android/xml/camera_provider_paths.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
-->
1818

1919
<paths xmlns:android="http://schemas.android.com/apk/res/android">
20-
<cache-path name="cache_files" path="." />
20+
<cache-path name="cache_files" path="org.apache.cordova.camera/" />
2121
</paths>

0 commit comments

Comments
 (0)