有两种方法可以在webView中下载文件 -
1)通过webview下载
// download manager
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.setMimeType(mimeType);
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Downloading file...");
request.setTitle(URLUtil.guessFileName(url, contentDisposition,
mimeType));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
url, contentDisposition, mimeType));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File",
Toast.LENGTH_LONG).show();
}
});
// download manager
2)打开应用选择器,通过第三方应用下载
// download via...
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);
Toast.makeText(getApplicationContext(), "don't choose our app as it can't handle download intents, i have posted a question on stackoverflow though.",
Toast.LENGTH_SHORT).show();
}
});
// download via..
但我如何让他们两个一起工作?我希望我的应用程序显示一个对话框,为您提供在应用程序中下载或通过第三方应用程序下载的两个选项。
答案 0 :(得分:1)
您可以使用此代码段
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setMessage("How you want to download this file?")
.setCancelable(false)
.setPositiveButton("Use webView",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
//your code here for download manager
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.setMimeType(mimeType);
String cookies= CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie",cookies);
request.addRequestHeader("User-Agent",userAgent);
request.setDescription("Downloading file...");
request.setTitle(URLUtil.guessFileName(url,contentDisposition,
mimeType));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,URLUtil.guessFileName(
url,contentDisposition,mimeType));
DownloadManager dm=(DownloadManager)getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(),"Downloading File",
Toast.LENGTH_LONG).show();} });
dismiss();}})
.setNegativeButton("3rd Party App",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
// download via...
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);
Toast.makeText(getApplicationContext(),"don't choose our app as it can't handle download intents, i have posted a question on stackoverflow though.",Toast.LENGTH_SHORT).show(); }});dialog.cancel();} });
AlertDialog alert=builder.create();
alert.show();
这是我添加的片段,它会显示两个选项,就像你只想把你的逻辑放在按钮的点击方法上一样。
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("How you want to download?")
.setCancelable(false)
.setPositiveButton("By web", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//use logic of downloading with web view here
dialog.cancel();
}
})
.setNegativeButton("Use third party tool", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//use logic of third party tool here
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();