Question:
Hi I'm developing an app with WebView
and I noticed that it's not possible to download files from the web.
I tried to download a file from dropbox,
It is possible that before downloading the file, an alert appears showing the following:
Want to download:
Nome-Do-Arquivo
andSim
andNão
buttons
Code already made:
package br.and.browser;
import ...
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().hide();
final EditText url = (EditText) findViewById(R.id.edittext_url);
final WebView myWebView = (WebView) findViewById(R.id.webview);
ImageButton Back = (ImageButton) findViewById(R.id.button_back);
ImageButton Next = (ImageButton) findViewById(R.id.button_next);
ImageButton Reload = (ImageButton) findViewById(R.id.button_reload);
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.getSettings().getBuiltInZoomControls();
myWebView.invokeZoomPicker();
myWebView.loadUrl("https://www.google.com.br/");
Back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myWebView.goBack();
}
});
Next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myWebView.goForward();
}
});
Reload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myWebView.reload();
}
});
url.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = true;
if (actionId == EditorInfo.IME_ACTION_GO) {
myWebView.loadUrl(url.getText().toString());
return true;
}
return true;
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
WebView myWebView = (WebView) findViewById(R.id.webview);
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Answer:
I got it with setDownloadListener
, but in the android 6+
version android 6+
the app stops working!
Method for PDF download
wv.setDownloadListener(new DownloadListener() {
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); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "boleto.pdf");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); //This is important!
intent.addCategory(Intent.CATEGORY_OPENABLE); //CATEGORY.OPENABLE
intent.setType("*/*");//any application,any extension
Toast.makeText(getApplicationContext(), "Baixando Arquivo", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
}
});