From e8c6a9cbf9c5c33c837a214a3ca3a1effe06f16a Mon Sep 17 00:00:00 2001 From: Ramil Gallyamov Date: Thu, 3 Mar 2022 20:57:25 +0300 Subject: [PATCH 1/2] workaround for handling storage updates in Android 11 --- src/android/SaveImageGallery.java | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/android/SaveImageGallery.java b/src/android/SaveImageGallery.java index 392ae20..d9c3f08 100644 --- a/src/android/SaveImageGallery.java +++ b/src/android/SaveImageGallery.java @@ -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; @@ -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; @@ -243,7 +237,7 @@ 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"); @@ -251,4 +245,4 @@ public void onRequestPermissionResult(int requestCode, String[] permissions, int break; } } -} \ No newline at end of file +} From 8fb3f6dd890cfdde2708a8bfd63e4cfceb67b9b2 Mon Sep 17 00:00:00 2001 From: Ramil Gallyamov Date: Thu, 3 Mar 2022 20:58:20 +0300 Subject: [PATCH 2/2] 0.0.27 --- package.json | 2 +- plugin.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index b6db2f1..ea17163 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/plugin.xml b/plugin.xml index 3bfd966..b36acc1 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,5 +1,5 @@ - +