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

Added support for Android SDK 33 and prior versions #266

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

<header-file src="src/ios/GMImagePicker/FeHourGlass.h" />
<source-file src="src/ios/GMImagePicker/FeHourGlass.m" />

<header-file src="src/ios/GMImagePicker/UIImage+fixOrientation.h" />
<source-file src="src/ios/GMImagePicker/UIImage+fixOrientation.m" />

Expand Down Expand Up @@ -108,6 +108,7 @@

<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
</config-file>

<preference name="ANDROID_SUPPORT_V7_VERSION" default="27.+"/>
Expand Down Expand Up @@ -139,7 +140,10 @@
<resource-file src="src/android/Library/res/layout/actionbar_done_button.xml" target="res/layout/actionbar_done_button.xml"/>
<resource-file src="src/android/Library/res/layout/multiselectorgrid.xml" target="res/layout/multiselectorgrid.xml"/>
<resource-file src="src/android/Library/res/values/multiimagechooser_strings_en.xml" target="res/values/multiimagechooser_strings_en.xml"/>
<resource-file src="src/android/Library/res/values/themes.xml" target="res/values/themes.xml"/>
<config-file target="res/values/themes.xml" parent="/resources">
<style name="MyTheme" parent="android:Theme.Holo.Light">
</style>
</config-file>

<resource-file src="src/android/Library/res/values-de/multiimagechooser_strings_de.xml" target="res/values-de/multiimagechooser_strings_de.xml"/>
<resource-file src="src/android/Library/res/values-es/multiimagechooser_strings_es.xml" target="res/values-es/multiimagechooser_strings_es.xml"/>
Expand Down
34 changes: 22 additions & 12 deletions src/android/com/synconset/ImagePicker/ImagePicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class ImagePicker extends CordovaPlugin {

Expand Down Expand Up @@ -77,13 +77,13 @@ public boolean execute(String action, final JSONArray args, final CallbackContex
// some day, when everybody uses a cordova version supporting 'hasPermission', enable this:
/*
if (cordova != null) {
if (cordova.hasPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
if (cordova.hasPermission(Manifest.permission.READ_MEDIA_IMAGES)) {
cordova.startActivityForResult(this, imagePickerIntent, 0);
} else {
cordova.requestPermission(
this,
PERMISSION_REQUEST_CODE,
Manifest.permission.READ_EXTERNAL_STORAGE
Manifest.permission.READ_MEDIA_IMAGES
);
}
}
Expand All @@ -104,17 +104,27 @@ public boolean execute(String action, final JSONArray args, final CallbackContex

@SuppressLint("InlinedApi")
private boolean hasReadPermission() {
return Build.VERSION.SDK_INT < 23 ||
PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(this.cordova.getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
return (Build.VERSION.SDK_INT < 23 || (
Build.VERSION.SDK_INT >= 33
? PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(this.cordova.getActivity(), Manifest.permission.READ_MEDIA_IMAGES)
: PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(this.cordova.getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)
));
}

@SuppressLint("InlinedApi")
private void requestReadPermission() {
if (!hasReadPermission()) {
ActivityCompat.requestPermissions(
this.cordova.getActivity(),
new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},
PERMISSION_REQUEST_CODE);
if (Build.VERSION.SDK_INT >= 33) {
ActivityCompat.requestPermissions(
this.cordova.getActivity(),
new String[] {Manifest.permission.READ_MEDIA_IMAGES},
PERMISSION_REQUEST_CODE);
} else {
ActivityCompat.requestPermissions(
this.cordova.getActivity(),
new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},
PERMISSION_REQUEST_CODE);
}
}
// This method executes async and we seem to have no known way to receive the result
// (that's why these methods were later added to Cordova), so simply returning ok now.
Expand All @@ -125,9 +135,9 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && data != null) {
int sync = data.getIntExtra("bigdata:synccode", -1);
final Bundle bigData = ResultIPC.get().getLargeData(sync);

ArrayList<String> fileNames = bigData.getStringArrayList("MULTIPLEFILENAMES");

JSONArray res = new JSONArray(fileNames);
callbackContext.success(res);

Expand Down