This repository was archived by the owner on Jul 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathCanvas2ImagePlugin.java
More file actions
146 lines (120 loc) · 4.19 KB
/
Copy pathCanvas2ImagePlugin.java
File metadata and controls
146 lines (120 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package org.devgeeks.Canvas2ImagePlugin;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Calendar;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
import java.io.StringBufferInputStream;
import android.util.Base64InputStream;
/**
* Canvas2ImagePlugin.java
*
* Android implementation of the Canvas2ImagePlugin for iOS.
* Inspirated by Joseph's "Save HTML5 Canvas Image to Gallery" plugin
* http://jbkflex.wordpress.com/2013/06/19/save-html5-canvas-image-to-gallery-phonegap-android-plugin/
*
* @author Vegard Løkken <vegard@headspin.no>
*/
public class Canvas2ImagePlugin extends CordovaPlugin {
public static final String ACTION = "saveImageDataToLibrary";
@Override
public boolean execute(final String action, final JSONArray data,
final CallbackContext callbackContext) throws JSONException {
if (action.equals(ACTION)) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
StringBufferInputStream inStream = null;
// Anonymous block to allow GC collection asap if necessary.
{
String base64 = data.optString(0);
if (base64.equals("")) {
// isEmpty() requires API level 9
callbackContext.error("Missing base64 string");
return;
}
inStream = new StringBufferInputStream(base64);
}
Base64InputStream b64in = new Base64InputStream(inStream, 0);
// Create the bitmap from the base64 string
//Log.d("Canvas2ImagePlugin ", Integer.toString(base64.length()));
//byte[] decodedString = Base64.decode(base64, Base64.DEFAULT);
//Bitmap bmp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
Bitmap bmp = BitmapFactory.decodeStream(b64in);
if (bmp == null) {
callbackContext.error("The image could not be decoded");
} else {
// 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());
}
}
});
return true;
} else {
return false;
}
}
private File savePhoto(Bitmap bmp) {
File retVal = null;
try {
Calendar c = Calendar.getInstance();
String date = "" + c.get(Calendar.DAY_OF_MONTH)
+ c.get(Calendar.MONTH)
+ c.get(Calendar.YEAR)
+ c.get(Calendar.HOUR_OF_DAY)
+ c.get(Calendar.MINUTE)
+ c.get(Calendar.SECOND);
String deviceVersion = Build.VERSION.RELEASE;
Log.i("Canvas2ImagePlugin", "Android version " + deviceVersion);
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) {
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;
} catch (Exception e) {
Log.e("Canvas2ImagePlugin", "An exception occured while saving image: "
+ e.toString());
}
return retVal;
}
/* 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);
cordova.getActivity().sendBroadcast(mediaScanIntent);
}
}