Java 使用DownloadManager类从WebView下载文件
我在这里有一个小代码,当点击webview上的链接时,它是指向文件的链接,在本例中是.mp4。此代码将转到默认web浏览器并请求可以查看此文件类型的应用程序Java 使用DownloadManager类从WebView下载文件,java,android,android-intent,webview,Java,Android,Android Intent,Webview,我在这里有一个小代码,当点击webview上的链接时,它是指向文件的链接,在本例中是.mp4。此代码将转到默认web浏览器并请求可以查看此文件类型的应用程序 myWebView.setDownloadListener(new DownloadListener() { public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,
myWebView.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);
}
});
我想要的是,当我点击该文件链接时,它会创建一个对话框,要求weather下载或查看该文件。如果单击下载,我希望使用DownloadManager类来处理它,并在后台下载该文件,完成后发出警报。如果单击“查看”,我想创建一个请求应用程序的意图,该应用程序无需进入web浏览器即可查看此文件
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, final String url) {
if (url.endsWith(".mp4")) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.dialog_title)
.setCancelable(false)
.setPositiveButton(R.string.dialog_download, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Some descrition");
request.setTitle("Some title");
// in order for this if to run, you must use the android 3.2 to compile your app
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "name-of-the-file.ext");
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
})
.setNegativeButton(R.string.dialog_play, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "video/mp4");
startActivity(intent);
}
});
}
});
AlertDialog alert = builder.create();
alert.show();
}
return false;
}
}
现在我得到了提示用户下载或播放用户单击的mp4文件的代码。但是,当我点击播放或下载它是工作,直到我点击链接的第二次,有什么问题,上面的代码,如果有人可以纠正这一点,请。谢谢
我对Android开发和java非常陌生,如果有人能指导我,这将帮助我更快地学习。同时也为我的英语感到抱歉…您的问题的解决方案依赖于拦截您的webview试图加载的URL。创建并覆盖shouldOverrideUrlLoading方法:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith(".mp3")) { //or whatever other extension
//Prompt user for action (save or view)
}
return false;
}
然后,根据用户的选择,启动一个来处理下载文件,或者启动查看意图。
if(url.endsWith(“.mp4”){AlertDialog.Builder Builder=new AlertDialog.Builder(?);Builder.setMessage(R.string.dialog_title)。setCancelable(false)。setPositiveButton(R.string.dialog_下载,新建DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog,int?{//我如何将异步放入下载?}}})。setNegativeButton(R.string.dialog_播放,新建DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog,int?){//如何创建查看mp4的意图?}});AlertDialog alert=builder.create();返回false;}
按照我提供的链接使用asynctask进行下载。创建查看mp4的意图可以像这样完成:我只是用代码更新问题,以提示用户下载并播放文件,但有一些问题,当我单击链接时,直到第二次单击链接才起作用,请帮助c纠正那个密码。