1

So I have a website that generate a dataUrl csv, now I want to open that website using android webView. Everything works great except for saving the dataUrl into csv. I can get this working in android browser, but not in webView. I have read a few posts (this one) saying that download manager does not work with dataURL in android webView, but since I'm very new to android webView I am not sure if I got it correctly.

Here is the main activity of my app

public class MainActivity extends AppCompatActivity {
private  WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try
    {
        this.getSupportActionBar().hide();
    }
    catch (NullPointerException e){}

    setContentView(R.layout.activity_main);

    webView = (WebView) findViewById(R.id.webview);

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setDomStorageEnabled(true);

    webView.setWebViewClient(( new WebViewClient()));

    webView.setWebChromeClient(new WebChromeClient() {

        @Override
        public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
            AlertDialog dialog = new AlertDialog.Builder(view.getContext()).
                    setTitle("Download").
                    setMessage(message).
                    setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //do nothing
                        }
                    }).create();
            dialog.show();
            result.confirm();
            return true;
        } });
    webView.setDownloadListener(new DownloadListener() {
        @Override
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);
        }
    });
    webView.loadUrl("myWebURLHere");
}}

On the logcat when I tried to download the dataUrl I got this error

java.lang.IllegalArgumentException: Can only download HTTP/HTTPS URIs: data:text/csv;charset=utf-8,%EF%BB%BF%22Time%22%2C%22PH%22%2C%22TDS%20(ppm)%22%0D%0A%221609303256%22%2C%227%22%2C%2225%22%0D%0A%221609331150%22%2C%220.00%22%2C%22600.00%22%0D%0A%221609331165%22%2C%227.33%22%2C%22600.00%22%0D%0A%221609331176%22%2C%227.33%22%2C%22600.00%22%0D%0A%221609331187%22%2C%227.33%22%2C%22600.00%22%0D%0A%221609331198%22%2C%227.33%22%2C%22600.00%22%0D%0A%221609331211%22%2C%227.33%22%2C%22600.00%22%0D%0A%221609331221%22%2C%227.33%22%2C%22600.00%22%0D%0A%221609331232%22%2C%227.33%22%2C%22600.00%22%0D%0A%221609331243%22%2C%227.33%22%2C%22600.00%22%0D%0A%221609331254%22%2C%227.33%22%2C%22600.00%22%0D%0A%221609331265%22%2C%227.33%22%2C%22600.00%22%0D%0A%221609331276%22%2C%227.33%22%2C%22600.00%22%0D%0A%221609331286%22%2C%227.33%22%2C%22600.00%22%0D%0A%221609332484%22%2C%2225.00%22%2C%22100.00%22%0D%0A%221609332499%22%2C%2225.00%22%2C%22100.00%22%0D%0A%221609332509%22%2C%2225.00%22%2C%22100.00%22%0D%0A%221609332520%22%2C%2225.00%22%2C%22100.00%22%0D%0A%221609332531%22%2C%2225.00%22%2C%22100.00%22%0D%0A%221609332541%22%2C%2225.00%22%2C%22100.00%22%0D%0A%221609332552%22%2C%2225.00%22%2C%22100.00%22%0D%0A%221609332562%22%2C%2225.00%22%2C%22100.00%22%0D%0A%221609332573%22%2C%2225.00%22%2C%22100.00%22%0D%0A
    at android.app.DownloadManager$Request.<init>(DownloadManager.java:529)

Is there anyway that I can save the dataUrl to my phone through the webView app?

shafiyyah
  • 61
  • 1
  • 6

0 Answers0