0

I'm trying to send the photo image via Email and sms in phonegap, so written the plugin which acts as a bridge to java native code. the issue is when i click share button from html "Class not found" alert is poped up. even though the plugin name is correctly declared in config.xml file i'm getting this error, please help me..

This is the java code for it.

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+ "/Download/", "imageee.png");

                try {
                outStream = new FileOutputStream(file);
                bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                outStream.flush();
                outStream.close();
                } catch (FileNotFoundException e) {
                    System.out.println(" praise god........");
                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;
    }
  }

Share.js plugin

 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();
}

Declaration of plugin in config.xml file

<plugins>
    <plugin name="Share" value="com.picsswipe.Share"/>
</plugins>

logcat

 11-11 16:02:04.898: W/System.err(15149): java.lang.InstantiationException: com.picsswipe.Share
 11-11 16:02:04.898: W/System.err(15149):   at java.lang.Class.newInstanceImpl(Native Method)
 11-11 16:02:04.898: W/System.err(15149):   at java.lang.Class.newInstance(Class.java:1409)
 11-11 16:02:04.898: W/System.err(15149):   at org.apache.cordova.api.PluginEntry.createPlugin(PluginEntry.java:80)
 11-11 16:02:04.898: W/System.err(15149):   at org.apache.cordova.api.PluginManager.getPlugin(PluginManager.java:249)
 11-11 16:02:04.898: W/System.err(15149):   at org.apache.cordova.api.PluginManager.exec(PluginManager.java:206)
 11-11 16:02:04.898: W/System.err(15149):   at org.apache.cordova.ExposedJsApi.exec(ExposedJsApi.java:51)
 11-11 16:02:04.898: W/System.err(15149):   at android.webkit.WebViewCore.nativeHandleTouchEvent(Native Method)
 11-11 16:02:04.898: W/System.err(15149):   at android.webkit.WebViewCore.nativeHandleTouchEvent(Native Method)
 11-11 16:02:04.898: W/System.err(15149):   at android.webkit.WebViewCore.access$6200(WebViewCore.java:54)
 11-11 16:02:04.898: W/System.err(15149):   at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1658)
11-11 16:02:04.898: W/System.err(15149):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-11 16:02:04.898: W/System.err(15149):    at android.os.Looper.loop(Looper.java:130)
11-11 16:02:04.898: W/System.err(15149):    at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:685)
 11-11 16:02:04.898: W/System.err(15149):   at java.lang.Thread.run(Thread.java:1019)
 11-11 16:02:04.898: I/System.out(15149): Error adding plugin com.picsswipe.Share.

onclick function of html file

instance.addEventHandler(PhotoSwipe.EventTypes.onToolbarTap, function(e){
                    if (e.toolbarAction === PhotoSwipe.Toolbar.ToolbarAction.none){
                            share();
                    }
                });




  function share() {  
        window.plugins.share.show({path: "Image"},
            function(e) {
            alert(e)    }, // Success function
            function() {
                alert("Praise god :( ")
            },// Failure function
            imagename 
        );
    }
DD.
  • 973
  • 2
  • 10
  • 32
  • Which version of phonegap your using and give code for package, import list in Share.java, html function call. so that we can find the issue easily – power_scriptor Nov 11 '13 at 12:34
  • thanks for the reply, updated my question, i'm using cordova.2.9 @power_scriptor – DD. Nov 11 '13 at 12:47

1 Answers1

0

Please make those changes. Here I put all your js code

    var Share = function() {};
Share.prototype.show = function(success, fail, path) {
    return PhoneGap.exec(function(args) {success(args);}, function(args) {fail(args);}, 'Share', 'ShareImage', [{"image": path}]);
};
    PhoneGap.addConstructor(function() {
        PhoneGap.addPlugin("share", new Share());
    });

instance.addEventHandler(PhotoSwipe.EventTypes.onToolbarTap, function (e) {
        if (e.toolbarAction === PhotoSwipe.Toolbar.ToolbarAction.none) {
            share();
        }
    });
    function share() {
        var imagename = ""; // Please give your image name here or page from share() function
        Share.prototype.show({path: "Image"},function (e) {alert(e);},function () {alert("Praise god :( ");},imagename);
    }

config.xml

<feature name="Share">
   <param name="android-package" value="$Share.java file package name$"/>
</feature>

Example:

<feature name="Share">
    <param name="android-package" value="org.apache.cordova.plugin.Share"/>
</feature>

For best approach you can create package name package org.apache.cordova.plugin; for all your phonegap plugin.

I didn't check your Share.java file. And I already share my working sms plugin code HERE

Please let me know you have any issue.

Please remove the below code

public Share(Context context) {
    this.context = context;
}
Community
  • 1
  • 1
power_scriptor
  • 3,274
  • 1
  • 15
  • 16
  • thanks for taking time to reply me, i applied the above but its not working, i'm getting this error "11-11 19:22:50.921: I/Web Console(21576): Failed to run constructor: TypeError: Object # has no method 'addPlugin' at file:///android_asset/www/js/cordova.js:317 " – DD. Nov 11 '13 at 13:54
  • Now see my code I updated two times. Are you updated java package name in your config.xml file. I would like to know what are the change you made and where – power_scriptor Nov 11 '13 at 14:01
  • Re order the js code and remove constructor in Share.java file – power_scriptor Nov 11 '13 at 14:18