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

Make the Plugin work unser ios10 and Android >= 23 #74

Open
wants to merge 2 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: 8 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<js-module src="www/Canvas2ImagePlugin.js" name="Canvas2ImagePlugin">
<clobbers target="window.canvas2ImagePlugin" />
</js-module>

<dependency id="cordova-plugin-compat" version="^1.0.0" />

<!-- ios -->
<platform name="ios">
Expand All @@ -32,6 +34,12 @@

<source-file src="src/ios/Canvas2ImagePlugin.m"
compiler-flags="-fno-objc-arc" />

<preference name="PHOTOLIBRARY_USAGE_DESCRIPTION" default=" " />
<config-file target="*-Info.plist" parent="NSPhotoLibraryUsageDescription">
<string>$PHOTOLIBRARY_USAGE_DESCRIPTION</string>
</config-file>

</platform>

<!-- android -->
Expand Down
94 changes: 72 additions & 22 deletions src/android/Canvas2ImagePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PermissionHelper;

import org.json.JSONArray;
import org.json.JSONException;
Expand All @@ -18,6 +19,9 @@
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
import android.Manifest;
import android.content.pm.PackageManager;


/**
* Canvas2ImagePlugin.java
Expand All @@ -30,6 +34,10 @@
*/
public class Canvas2ImagePlugin extends CordovaPlugin {
public static final String ACTION = "saveImageDataToLibrary";
private final String WRITE_EXTERNAL_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE;
public static final int WRITE_PERM_REQUEST_CODE = 1;
private CallbackContext callbackContext;
private Bitmap bmp;

@Override
public boolean execute(String action, JSONArray data,
Expand All @@ -40,35 +48,48 @@ public boolean execute(String action, JSONArray data,
String base64 = data.optString(0);
if (base64.equals("")) // isEmpty() requires API level 9
callbackContext.error("Missing base64 string");

// Create the bitmap from the base64 string
Log.d("Canvas2ImagePlugin", base64);
byte[] decodedString = Base64.decode(base64, Base64.DEFAULT);
Bitmap bmp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
if (bmp == null) {
callbackContext.error("The image could not be decoded");
} else {


this.bmp = bmp;
this.callbackContext = callbackContext;
// Save the image
File imageFile = savePhoto(bmp);
if (imageFile == null)
callbackContext.error("Error while saving image");

// Update image gallery
scanPhoto(imageFile);

callbackContext.success(imageFile.toString());
askPermissionAndSave();

}

return true;
} else {
return false;
}
}

private File savePhoto(Bitmap bmp) {
File retVal = null;

private void askPermissionAndSave() {

if (PermissionHelper.hasPermission(this, WRITE_EXTERNAL_STORAGE)) {
Log.d("SaveImage", "Permissions already granted, or Android version is lower than 6");
savePhoto();
} else {
Log.d("SaveImage", "Requesting permissions for WRITE_EXTERNAL_STORAGE");
PermissionHelper.requestPermission(this, WRITE_PERM_REQUEST_CODE, WRITE_EXTERNAL_STORAGE);
}
}


private void savePhoto() {


File image = null;

Bitmap bmp = this.bmp;
CallbackContext callbackContext = this.callbackContext;

try {
Calendar c = Calendar.getInstance();
String date = "" + c.get(Calendar.DAY_OF_MONTH)
Expand All @@ -91,36 +112,65 @@ private File savePhoto(Bitmap bmp) {
if (check >= 1) {
folder = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

if(!folder.exists()) {
folder.mkdirs();
}
} else {
folder = Environment.getExternalStorageDirectory();
}

File imageFile = new File(folder, "c2i_" + date.toString() + ".png");

FileOutputStream out = new FileOutputStream(imageFile);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();

retVal = imageFile;
image = imageFile;
} catch (Exception e) {
Log.e("Canvas2ImagePlugin", "An exception occured while saving image: "
+ e.toString());
}
return retVal;

if (image == null) {
callbackContext.error("Error while saving image");
} else {
// Update image gallery
scanPhoto(image);
callbackContext.success(image.toString());
}


}
/* Invoke the system's media scanner to add your photo to the Media Provider's database,

/* Invoke the system's media scanner to add your photo to the Media Provider's database,
* making it available in the Android Gallery application and to other apps. */
private void scanPhoto(File imageFile)
{
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(imageFile);
mediaScanIntent.setData(contentUri);
mediaScanIntent.setData(contentUri);
cordova.getActivity().sendBroadcast(mediaScanIntent);
}
}

/**
* Callback from PermissionHelper.requestPermission method
*/
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
for (int r : grantResults) {
if (r == PackageManager.PERMISSION_DENIED) {
Log.d("SaveImage", "Permission not granted by the user");
callbackContext.error("Permissions denied");
return;
}
}

switch (requestCode) {
case WRITE_PERM_REQUEST_CODE:
Log.d("SaveImage", "User granted the permission for WRITE_EXTERNAL_STORAGE");
savePhoto();
break;
}
}
}