Issue
Is it possible, by any chance to invoke Android's native share(file) functionality within Chrome WebView? I'm creating a hybrid Ionic AngularJS app, and would like to use the Android's native share functionality for files. I've tried different plugins, but all of them use custom share implementation for each of the apps present on the device, which does not suffice.
Solution
Did you tried it to making an JavascriptInterface
Add this in your MainActivity.java
public class WebViewJavaScriptInterface {
private Context context;
@JavascriptInterface
public void sharingIsCaring(String code) {
openSharing(code);
}
}
This is the action that starts the sharing:
public void openSharing(String code) {
String message = "Your share message: " + code;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(share, "Share your code"));
}
So, how do you trigger the @JavascriptInterface
You need to add this in your OnCreate()
method:
view.addJavascriptInterface(new WebViewJavaScriptInterface(this), "SmashIt");
So to trigger it from the JS you only need to call the function sharingIsCaring()
You need to do that by placing SmashIt
for the function that you call so Android will know it's him you are calling. So it will look like this:
SmashIt.sharingIsCaring(code)
Answered By - Steven
Answer Checked By - Mildred Charles (JavaFixing Admin)