I want to download a file (e.g. .mp3) from a website using webview but the problem is that whenever I click on the link, it will open a browser (by default) which appears for a second before It closes. and the files did not load.
Here is my code,
import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebViewClient; import android.webkit.WebChromeClient; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.webkit.DownloadListener; import android.widget.Button; import android.widget.TextView; public class Main extends Activity { WebView webview; Button bt_search; TextView txt_search; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); webview = (WebView) findViewById(R.id.webView); webview.setWebChromeClient(new WebChromeClient()); webview.getSettings().setJavaScriptEnabled(true); 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); } }); txt_search = (TextView) findViewById(R.id.song); webview.loadUrl("http://www.google.com"); bt_search = (Button) findViewById(R.id.findit); bt_search.setOnClickListener(new OnClickListener() { public void onClick(View v) { String keyword = txt_search.getText().toString().trim(); if (!keyword.equals("")) { webview.loadUrl("MP3 Sites" + keyword + ".html"); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) {
java android download webview mp3
Gleich zi libertia
source share