I have the problem that on emulator some things in my app doesn't work. The thing is onDownloadListener()
method. In real device, it runs perfectly: I can download image and send it by messenger e.g. In the emulator it doesn't work, the method just doesn't run itself. It is terrible, I have tried different emulators and different devices. On each device I tried it was working but none of the emulators can run this code:
public class MyExport implements DownloadListener {
private final WebView webView;
private Context context;
private Activity activity;
public MyExport(Context c, Activity a, WebView webView) {
this.activity = a;
this.context = c;
this.webView = webView;
}
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
System.out.println("IM IN onDwoanloadStart");
String intentType = "image/png";
String fileName = "img.png";
try {
if(url != null) {
FileOutputStream fos;
fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
//conversion stuff
fos.write(decodedStr);
fos.getFD().sync();
fos.flush();
fos.close();
Intent sendIntent = new Intent();
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.setAction(Intent.ACTION_SEND);
File filePath = new File(String.valueOf(context.getFilesDir()));
File newFile = new File(filePath, fileName);
sendIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".FileProvider", newFile));
sendIntent.setType(intentType);
context.startActivity(sendIntent);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@JavascriptInterface
public void runExport(){
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
webView.loadUrl("javascript: obj.exportImage()");
}
});
}
And this is how I add it to the webView
:
MyExport export = new MyExport(activity.getBaseContext(), activity, this.webView);
this.webView.setDownloadListener(export);
this.webView.addJavascriptInterface(export, "jsint");
When I click on the button in WebView
the exportImage()
from Javascript
is called but the onDownloadListener
is not. This is happening ONLY on emulators!