Vũ Ngây Thơ Vũ Ngây Thơ - 2 months ago 18
Android Question

Can not download in Android webview


Possible Duplicate:

Download File inside WebView




I have a WebView on my screen:

webDownloadTicket = (WebView) findViewById(R.id.webViewToDownload);
webDownloadTicket.getSettings().setJavaScriptEnabled(true);
webDownloadTicket.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
// and load url
webDownloadTicket.loadUrl("xxx.com/TicketWEB/download.jsp");
// setup for download listener
webDownloadTicket.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);

}
});


I was testing with browser, going to this URL
and then press the download button, it will download a JSON file.

But when I test on my Android app with that code, it can load the web page but can not download anything when I press download button.

Answer Source

The most common answer for not being able to access network from an app is a missing permission-declaration in AndroidManifest.xml. Check the file for this line and add it if it's not there:

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