我已经编写了用于从 HTML 中的 carrousel 滑块图像中获取当前页面的代码,并使用 Android 中的 Java 为 phonegap 项目通过电子邮件和 MMS 共享它。
这是我的 Java 代码:
public class Share extends CordovaPlugin {
private FileOutputStream outStream;
private File file;
Bitmap bm;
public static final String ACTION_POSITION = "ShareImage";
Context context;
public Share(Context context) {
this.context = context;
}
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
throws JSONException {
if (ACTION_POSITION.equals(action)) {
try {
JSONObject arg_object = args.getJSONObject(0);
Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
sendIntent.setType("image/jpg");
String uri = "@drawable/"+arg_object.getString("image")+".jpg";
int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
bm = BitmapFactory.decodeResource( context.getResources(), imageResource);
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
file = new File(extStorageDirectory, "image.png");
try {
outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, arg_object.getString("image"));
this.cordova.getActivity().startActivity(sendIntent);
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
callbackContext.error(e.getMessage());
return false;
}
}
return true;
}
}
一切正常,但是当我单击“共享”按钮时,出现“找不到类”的异常。问题出在上下文类中,尝试通过清理并删除 Google 中给出的 gen 文件,但没有任何结果。请帮我解决这个问题。
日志输出:
11-09 20:30:44.156: W/System.err(2842): java.lang.InstantiationException: com.picsswipe.Share
11-09 20:30:44.156: W/System.err(2842): at java.lang.Class.newInstanceImpl(Native Method)
11-09 20:30:44.156: W/System.err(2842): at java.lang.Class.newInstance(Class.java:1409)
11-09 20:30:44.156: W/System.err(2842): at org.apache.cordova.api.PluginEntry.createPlugin(PluginEntry.java:80)
11-09 20:30:44.164: W/System.err(2842): at org.apache.cordova.api.PluginManager.getPlugin(PluginManager.java:249)
11-09 20:30:44.164: W/System.err(2842): at org.apache.cordova.api.PluginManager.exec(PluginManager.java:206)
11-09 20:30:44.164: W/System.err(2842): at org.apache.cordova.ExposedJsApi.exec(ExposedJsApi.java:51)
11-09 20:30:44.164: W/System.err(2842): at android.webkit.WebViewCore.nativeHandleTouchEvent(Native Method)
11-09 20:30:44.164: W/System.err(2842): at android.webkit.WebViewCore.nativeHandleTouchEvent(Native Method)
11-09 20:30:44.164: W/System.err(2842): at android.webkit.WebViewCore.access$6200(WebViewCore.java:54)
11-09 20:30:44.164: W/System.err(2842): at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1658)
11-09 20:30:44.164: W/System.err(2842): at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 20:30:44.164: W/System.err(2842): at android.os.Looper.loop(Looper.java:130)
11-09 20:30:44.164: W/System.err(2842): at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:685)
11-09 20:30:44.164: W/System.err(2842): at java.lang.Thread.run(Thread.java:1019)
share.js
插入:
var Share = function() {};
Share.prototype.show = function(success, fail, path) {
return cordova.exec( function(args) {
success(args);
}, function(args) {
fail(args);
}, 'Share', 'ShareImage', [{"image": path}]);
};
if(!window.plugins) {
window.plugins = {};
}
if (!window.plugins.share) {
window.plugins.share = new Share();
}
config.xml
:
<feature name="Share">
<param name="android-package" value="com.picsswipe.Share"/>
</feature>