Hi Joe,
Add this code on camera mapping:prereturn window.atob(value);/pre
If this photo is mapped to a localStorage variable then encoded base64 string will be saved in it. Then you can decode this string back using: prewindow.btoa/pre
Hi Joe,
Add this code on camera mapping:prereturn window.atob(value);/pre
If this photo is mapped to a localStorage variable then encoded base64 string will be saved in it. Then you can decode this string back using: prewindow.btoa/pre
Hi Joe,
window.btoa is a function, too. Here is how to use it in mapping:prereturn window.btoa(value);/pre
But on your first screenshot there is no need to add any code to an image asset.
I'm sorry Katya, I'm confused on how to incorporate these functions to get the desired results for my app. Do both of these functions need to be included in the JS for the "cameraimage" localStorage variable? I'm afraid you'll have to fully explain the process to me (if that's alright) on how to make this plug in work for me. Where does this code go, what values do I put in place of (value) and what additional steps need to be taken. This is the final step in my app development process for now, before I do extensive testing and perhaps release my first app ever onto the Android Marketplace.
As always, thanks so much for your patience, understanding, and extensive help.
Hello! Camera service returns string in base64 format. In your mapping there is cameraimage variable which you can save in DB or send users.
I guess what I'm saying is, I don't know how to use the atob() or btoa() function. With my current data mapping for cameraservice (below) nothing happens when I click "take a picture" button. I guess you can tell that I was trying to map the service so that it saves the image in a localStorage variable, so that when I click "share to twitter" button, it asks me to select that image from localStorage so that I can share it however I wish. Does this make sense? How would I do this? Is the atob() and btoa() function(s) the way to do this by utilizing the Verbruggen plugin?
Hi Joe,
Where we can find 'share' button in your app?
Hey Katya,
The 'share' button is on the 'camera' page. The screenshot below shows that there is an error in the "click" action javascript. You've already seen the data mapping I tried to do on the mobilecamera1 service event. I'm sure that's incorrect also.
I've no idea what type of javascript needs to go on "click" action with the "share" button on the "results" page. How would I store the contents (or graphs) from the results page so that they could be shared?
Hello! Working on it, I'll update.
Hi Joe,
1) In file SocialSharing.java should be:prepackage nl.xservices.plugins;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.util.Base64;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.apache.http.util.ByteArrayBuffer;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class SocialSharing extends CordovaPlugin {
private static final String ACTION_AVAILABLE_EVENT = "available";
private static final String ACTION_SHARE_EVENT = "share";
private static final String ACTION_CAN_SHARE_VIA = "canShareVia";
private static final String ACTION_SHARE_VIA = "shareVia";
private static final String ACTION_SHARE_VIA_TWITTER_EVENT = "shareViaTwitter";
private static final String ACTION_SHARE_VIA_FACEBOOK_EVENT = "shareViaFacebook";
private static final String ACTION_SHARE_VIA_WHATSAPP_EVENT = "shareViaWhatsApp";
private File tempFile;
private CallbackContext callbackContext;
@Override
public boolean execute(String action, JSONArray args, CallbackContext pCallbackContext) throws JSONException {
this.callbackContext = pCallbackContext;
try {
if (ACTION_AVAILABLE_EVENT.equals(action)) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
return true;
} else if (ACTION_SHARE_EVENT.equals(action)) {
return doSendIntent(args.getString(0), args.getString(1), args.getString(2), args.getString(3), null, false);
} else if (ACTION_SHARE_VIA_TWITTER_EVENT.equals(action)) {
return doSendIntent(args.getString(0), args.getString(1), args.getString(2), args.getString(3), "twitter", false);
} else if (ACTION_SHARE_VIA_FACEBOOK_EVENT.equals(action)) {
return doSendIntent(args.getString(0), args.getString(1), args.getString(2), args.getString(3), "facebook", false);
} else if (ACTION_SHARE_VIA_WHATSAPP_EVENT.equals(action)) {
return doSendIntent(args.getString(0), args.getString(1), args.getString(2), args.getString(3), "whatsapp", false);
} else if (ACTION_CAN_SHARE_VIA.equals(action)) {
return doSendIntent(args.getString(0), args.getString(1), args.getString(2), args.getString(3), args.getString(4), true);
} else if (ACTION_SHARE_VIA.equals(action)) {
return doSendIntent(args.getString(0), args.getString(1), args.getString(2), args.getString(3), args.getString(4), false);
} else {
callbackContext.error("socialSharing." + action + " is not a supported function. Did you mean '" + ACTION_SHARE_EVENT + "'?");
return false;
}
} catch (Exception e) {
callbackContext.error(e.getMessage());
return false;
}
}
private boolean doSendIntent(String message, String subject, String image, String url, String appPackageName, boolean peek) throws IOException {
final Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
final String dir = webView.getContext().getExternalFilesDir(null) + "/socialsharing-downloads";
createDir(dir);
sendIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Code: Select all
String localImage = image;
if ("".equals(image) || "null".equalsIgnoreCase(image)) {
sendIntent.setType("text/plain");
} else {
sendIntent.setType("image/*");
if (image.startsWith("http") || image.startsWith("www/")) {
final String filename = getFileName(image);
localImage = "file://" + dir + "/" + filename;
if (image.startsWith("http")) {
saveFile(getBytes(new URL(image).openConnection().getInputStream()), dir, filename);
} else {
saveFile(getBytes(webView.getContext().getAssets().open(image)), dir, filename);
}
} else if (image.startsWith("data:")) {
// image looks like this: data:image/png;base64,R0lGODlhDAA...
final String encodedImg = image.substring(image.indexOf(";base64,") + 8);
// the filename needs a valid extension, so it renders correctly in target apps
final String imgExtension = image.substring(image.indexOf("image/") + 6, image.indexOf(";base64"));
final String fileName = "image." + imgExtension;
saveFile(Base64.decode(encodedImg, Base64.DEFAULT), dir, fileName);
localImage = "file://" + dir + "/" + fileName;
} else if (!image.startsWith("file://")) {
throw new IllegalArgumentException("URL_NOT_SUPPORTED");
}
sendIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse(localImage));
}
if (!"".equals(subject) && !"null".equalsIgnoreCase(subject)) {
sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
}
// add the URL to the message, as there seems to be no separate field
if (!"".equals(url) && !"null".equalsIgnoreCase(url)) {
if (!"".equals(message) && !"null".equalsIgnoreCase(message)) {
message += " " + url;
} else {
message = url;
}
}
if (!"".equals(message) && !"null".equalsIgnoreCase(message)) {
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
}
if (appPackageName != null) {
final ActivityInfo activity = getActivity(sendIntent, appPackageName);
if (activity == null) {
return false;
}
if (peek) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
} else {
sendIntent.addCategory(Intent.CATEGORY_LAUNCHER);
sendIntent.setComponent(new ComponentName(activity.applicationInfo.packageName, activity.name));
this.cordova.startActivityForResult(this, sendIntent, 0);
}
} else {
if (peek) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
} else {
this.cordova.startActivityForResult(this, Intent.createChooser(sendIntent, null), 1);
}
}
return true; }
private ActivityInfo getActivity(final Intent shareIntent, final String appPackageName) {
final PackageManager pm = webView.getContext().getPackageManager();
List activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
if ((app.activityInfo.packageName).contains(appPackageName)) {
return app.activityInfo;
}
}
// no matching app found
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, getShareActivities(activityList)));
return null;
}
private JSONArray getShareActivities(List activityList) {
List packages = new ArrayList();
for (final ResolveInfo app : activityList) {
packages.add(app.activityInfo.packageName);
}
return new JSONArray(packages);
}
// cleanup after ourselves
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (tempFile != null) {
//noinspection ResultOfMethodCallIgnored
tempFile.delete();
}
// note that the resultCode needs to be sent correctly by the sharing app, which is not always the case
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, resultCode == Activity.RESULT_OK));
}
private void createDir(final String downloadDir) throws IOException {
final File dir = new File(downloadDir);
if (!dir.exists()) {
if (!dir.mkdirs()) {
throw new IOException("CREATE_DIRS_FAILED");
}
}
}
private String getFileName(String url) {
final int lastIndexOfSlash = url.lastIndexOf('/');
if (lastIndexOfSlash == -1) {
return url;
} else {
return url.substring(lastIndexOfSlash + 1);
}
}
private byte[] getBytes(InputStream is) throws IOException {
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int current;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
return baf.toByteArray();
}
private void saveFile(byte[] bytes, String dirName, String fileName) throws IOException {
final File dir = new File(dirName);
tempFile = new File(dir, fileName);
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(bytes);
fos.flush();
fos.close();
}
}/pre
2) To share the first graphic on Share button click there should be:prewindow.plugins.socialsharing.share(null, null,$('#plot1').jqplotToImageStr({}), null);/pre