2

I am building a phonegap-android application and as a part of the flow, I am calling an activity from my javascript side using javascriptInterface

This looks like

public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        super.init();
        appView.addJavascriptInterface(this, "Android");

        super.loadUrl(Config.getStartUrl());


}

now I also want to send back some data from my activity to the cordova webview.

I have defined a function in my javascript file which I want to call

function jsi_getData(data) {
     console.log("JSI GET IMAGE INVOKED ON JAVASCRIPT SIDE");
     alert(data);
 }

and in my android code, inside onActivityResult, I am calling this javascript function

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       if(requestCode == 12321){
             //do some work

           Log.i("MAIN_ACTIVITY", "***************  received data"+ data);
            appView.sendJavascript("javascript:jsi_getImage("+data+");");

        }
    }

This gives me the following error

09-16 18:18:27.211: I/Web Console(28991): processMessage failed: Message: Jjavascript:jsi_getData('data');:1034
09-16 18:18:27.211: I/Web Console(28991): processMessage failed: Error: ReferenceError: jsi_getImage is not defined:1035
09-16 18:18:27.211: I/Web Console(28991): processMessage failed: Stack: ReferenceError: jsi_getImage is not defined
09-16 18:18:27.211: I/Web Console(28991):     at eval (eval at processMessage (file:///android_asset/www/js/libs/cordova.js:996:26), <anonymous>:1:1)
09-16 18:18:27.211: I/Web Console(28991):     at processMessage (file:///android_asset/www/js/libs/cordova.js:996:13)
09-16 18:18:27.211: I/Web Console(28991):     at Function.androidExec.processMessages (file:///android_asset/www/js/libs/cordova.js:1063:13)
09-16 18:18:27.211: I/Web Console(28991):     at pollOnce (file:///android_asset/www/js/libs/cordova.js:933:17):1036
09-16 18:18:27.481: I/Adreno200-EGLSUB(28991): <ConfigWindowMatch:2087>: Format RGBA_8888.

I also changed aappView.sendJavascript() to this.sendJavascript() and I still have the same error Please help as I am litrally stuck!! This would be very helpful.

Thanks

1 Answer 1

1

Had a similar issue and I was going MENTAL with it, finally I found came up with an "UGLY" but working solution:

Instead of passing the data back from the onActivityResult do the following.

1) Define a global variable in your JAVA and store the result in that global variable

2) DO this in the onActivityResult:

Log.i("MAIN_ACTIVITY", "***************  received data"+ data);
appView.sendJavascript("jsi_getImage();");

3) Define another function in you JAVA something like:

public String send_picture() {
return previously_defined_global_variable;
}

4) In your Javascript function called jsi_getImage, make another call to send_picture() like:

function jsi_getImage(){
alert(window.MainActivity.send_picture());
}

Make sure that you are sending only string data between Java and Javascript (BASE64 encode for image)

Hope it helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.