0

I am creating a webview app, use to download .mp3 files into mobile phone using url link within the webview. Im a newbie in this arena. Many example ive seen, yet i dint understand because its a bit different from my current code. i got confused. So based on the code given..what code do i need to input so user can click on the url link within the webview to download any files especially .mp3 files into the internal or external memory?

(activitymain.xml)

<WebView android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />

(AndroidManifest.xml)

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>

(MainActivity.java)

webview = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);

webview.setWebViewClient(new WebViewClient());
webview.loadUrl("WEBLINK");

1 Answers1

0

Had you seen this question? I suppose that you should use DownloadListener for file downloading:

WebView.setDownloadListener:

Registers the interface to be used when content can not be handled by the rendering engine, and should be downloaded instead. This will replace the current handler.

So, your code should look like this:

webview.setDownloadListener(new DownloadListener() {
    public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    }
});
artem
  • 16,382
  • 34
  • 113
  • 189
  • Hi arts I did that but I get an error: see this: https://stackoverflow.com/questions/45780438/how-to-download-a-csv-file-created-in-js-to-your-android-phone-webview – Steven Aug 22 '17 at 08:32