0

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!

shurrok
  • 795
  • 2
  • 13
  • 43
  • have you tried debugging it? What happens when it is supposed to run the DownloadListener instead? Does it throw error logs? – Rafael T Sep 26 '17 at 11:40
  • I didnt try debugging, but I can say that console says nothing - no errors, no warnings – shurrok Sep 26 '17 at 11:41
  • Try to find out what happens on a download and dig into Android code if neccessary. You will have to find out what happens instead of your DownloadListener getting called – Rafael T Sep 26 '17 at 11:43
  • Well, my callback is working on the `Javascript` side but Android doesn't take the request, just nothing happens – shurrok Sep 26 '17 at 11:45
  • Then try to inject your own `WebViewClient` as suggested here: https://stackoverflow.com/questions/3926629/downloadlistener-not-working – Rafael T Sep 26 '17 at 11:46
  • As I said - I have my own `WebViewClient` – shurrok Sep 26 '17 at 11:46
  • but It is not in `webViewClient` (but i tried to move it there - the same issue) it is in external class named `myExport` which implements `DownloadListener` and I add this to `WebView` like this: `this.webView.setDownloadListener(myExpor); ` – shurrok Sep 26 '17 at 11:48
  • I cannot see you claiming using WebViewClient. Have you overwritten `shouldOverwriteUrlLoading` and checked there – Rafael T Sep 26 '17 at 11:49
  • I said it twice. I'm not using `shouldOverwriteUrlLoading` because my `DownlaodListener` is not there! – shurrok Sep 26 '17 at 11:52
  • Sorry, try ctrl+f - >shouldOverwriteUrlLoading. The only thing u find is in my last comment and your comment on that! WHERE did you say that TWICE? Also if you overwrite URL loading you do NOT need/ can delegate to your Dowload Manager yourself – Rafael T Sep 26 '17 at 11:54
  • I said in the question that my `onDownloadListner` is in the external class, and later in the comment and I'm talking about using `WebView`. My `shouldOverrideUrlLoading` is capable of different things that's why I can't use it. And whatsoever - my implementation doesn't work only on emulators. – shurrok Sep 26 '17 at 12:05
  • @RafaelT i updated the question to show how exactly my code is working – shurrok Sep 26 '17 at 12:15

0 Answers0