Skip to content

Commit

Permalink
Merge pull request #4 from gallyamow/master
Browse files Browse the repository at this point in the history
Storage updates in Android 11
  • Loading branch information
agomezmoron authored Mar 4, 2022
2 parents c6caf3d + 8fb3f6d commit 228aa34
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-save-image-gallery",
"version": "0.0.26",
"version": "0.0.27",
"description": "Cordova plugin to save base64 data as a png/jpg image into the device and applying compression if needed",
"license": "MIT",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns:android="http://schemas.android.com/apk/res/android" xmlns="http://www.phonegap.com/ns/plugins/1.0" id="cordova-save-image-gallery" version="0.0.26">
<plugin xmlns:android="http://schemas.android.com/apk/res/android" xmlns="http://www.phonegap.com/ns/plugins/1.0" id="cordova-save-image-gallery" version="0.0.27">

<engines>
<engine name="cordova-ios" version=">=3.8.0" />
Expand Down
28 changes: 11 additions & 17 deletions src/android/SaveImageGallery.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
} else {
Log.d("SaveImageGallery", "Requesting permissions for WRITE_EXTERNAL_STORAGE");
PermissionHelper.requestPermission(this, WRITE_PERM_REQUEST_CODE, WRITE_EXTERNAL_STORAGE);
}
}
}

return true;
Expand Down Expand Up @@ -161,31 +161,25 @@ private File savePhoto(Bitmap bmp, String prefix, String format, int quality) {
File retVal = null;

try {
String deviceVersion = Build.VERSION.RELEASE;
Calendar c = Calendar.getInstance();
String date = EMPTY_STR + c.get(Calendar.YEAR) + c.get(Calendar.MONTH) + c.get(Calendar.DAY_OF_MONTH)
+ c.get(Calendar.HOUR_OF_DAY) + c.get(Calendar.MINUTE) + c.get(Calendar.SECOND);

int check = deviceVersion.compareTo("2.3.3");

File folder;

/*
* File path = Environment.getExternalStoragePublicDirectory(
* Environment.DIRECTORY_PICTURES ); //this throws error in Android
* 2.2
*/
if (check >= 1) {
if (Build.VERSION.SDK_INT >= 30) {
// @see https://developer.android.com/about/versions/11/privacy/storage
folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) {
folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

if (!folder.exists()) {
folder.mkdirs();
}

} else {
folder = Environment.getExternalStorageDirectory();
}

if (!folder.exists()) {
folder.mkdirs();
}

// building the filename
String fileName = prefix + date;
Bitmap.CompressFormat compressFormat = null;
Expand Down Expand Up @@ -243,12 +237,12 @@ public void onRequestPermissionResult(int requestCode, String[] permissions, int
return;
}
}

switch (requestCode) {
case WRITE_PERM_REQUEST_CODE:
Log.d("SaveImageGallery", "User granted the permission for WRITE_EXTERNAL_STORAGE");
saveBase64Image(this._args, this._callback);
break;
}
}
}
}

0 comments on commit 228aa34

Please sign in to comment.