Thanks Katya! I'm pretty sure I did something incorrectly though. I added the above code into SocialSharing.java and it looked like this:
I get this in console in Eclipse:
What am I doing wrong? Thank you very much!!
Hello! Did you try to build .apk file through editor? Does it work?
Dear Joe,
You try to use Java code as JavaScript. You can find how to use Java code here: http://docs.appery.io/documentation/u...
Joe,
On the first screenshot I don't see Java code advised here: https://getsatisfaction.com/apperyio/... in the first section (you can compare a number of lines 'import' in the beginning of these files - they are different files).
I added that link's code to the "source" tab of the project in the picture below under Android-Pedal_Collective_Mobile-xservices-plugins-SocialSharing.java
precode
package 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);
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();
}
}
/code/pre
And when trying to export into .apk, I get this build error:
Hello! Sorry for delay, we're working on it...
Hello! Sorry, this code was pasted incorrectly https://getsatisfaction.com/apperyio/...
Please try your app now, should be working.
Here is the correct code https://gist.github.com/anonymous/882...
Thanks Maryna! It works now! It prompts me to choose where I would like to share the image. I tested Facebook, and it works. To share both "plot1" and "plot2" should I just have the code below on "share" button click on "results" page?
codewindow.plugins.socialsharing.share(null, null,$('#plot1').jqplotToImageStr({}), null);
window.plugins.socialsharing.share(null, null,$('#plot2').jqplotToImageStr({}), null);/code
When I click "back to record ride" on results page, I don't think anything happens. I get this in logcat:
When I click "take picture" button on "camera" page, nothing happens either. I get this in logcat:
When I click "share" on "camera" page, nothing happens when I have this code on click event:
codewindow.plugins.socialsharing.share(null, null,$('#cameraimage').jqplotToImageStr({}), null);/code