gpt4 book ai didi

android - 如何在webView中下载文件?

转载 作者:行者123 更新时间:2023-11-30 01:12:36 25 4
gpt4 key购买 nike

在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) 打开应用选择器以通过第 3 方应用下载-

 // 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..

但是我如何让它们一起工作呢?我希望我的应用显示一个对话框,为您提供两种选择:在应用内下载或通过第三部分应用下载。

最佳答案

你可以使用这个 fragment

    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();

这是我添加的 fragment ,它将向您显示两个选项,就像您想要的那样,只需将您的逻辑放在按钮的点击方法中即可。

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();

关于android - 如何在webView中下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38278292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com