如何使用下载管理器网页视图下载PDF / PPT / DOC文件到SD卡 [英] How to use a download Manager in a webview to download pdf/ppt/doc files to sdcard
问题描述
我的工作,它使用网页视图中打开我的网站(网址:learnportal.me)一个Android应用程序,然后在进入正确的凭据,它会加载其中有多个下载链接(这些普通的HTML页面都是PDF / PPT / DOC文件)。结果
所有这一切都发生在web视图中。
我要的是下载,直接在应用程序中发生的,没有任何重定向到移动设备默认的Web浏览器。
进一步,我所需要的文件被下载到SD卡
这是在MainActivity页我目前工作的......不知道它的正确的,虽然
公共类MainActivity延伸活动{
@燮pressWarnings(德precation)
@燮pressLint(SetJavaScriptEnabled)
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的WebView的WebView =新的WebView(本);
webview.setWebChromeClient(新WebChromeClient());
WebViewClient客户端=新ChildBrowserClient();
webview.setWebViewClient(客户端);
WebSettings设置= webview.getSettings();
settings.setJavaScriptEnabled(真);
//webview.setInitialScale(1);
//webview.getSettings().setUseWideViewPort(true);
settings.setJavaScriptCanOpenWindowsAutomatically(假);
//settings.setBuiltInZoomControls(true);
settings.setPluginState(PluginState.ON);
settings.setDomStorageEnabled(真);
webview.loadUrl(http://learnportal.me);
//webview.setId(5);
//webview.setInitialScale(0);
//webview.requestFocus();
// webview.requestFocusFromTouch();
的setContentView(web视图);
}
/ **
*本网页流量的客户端接收有关APPVIEW通知
* /
公共类ChildBrowserClient扩展WebViewClient {
@燮pressLint({InlinedApi,NewApi})
@覆盖
公共布尔shouldOverrideUrlLoading(的WebView视图,字符串URL){
布尔值= TRUE;
字符串延长= MimeTypeMap.getFileExtensionFromUrl(URL);
如果(延伸!= NULL){
MimeTypeMap哑剧= MimeTypeMap.getSingleton();
字符串mime类型= mime.getMimeTypeFromExtension(扩展);
如果(mime类型!= NULL){
如果(mimeType.toLowerCase()。包含(PPTX)
|| extension.toLowerCase()。包含(PDF)
|| extension.toLowerCase()。包含(DOC)
|| extension.toLowerCase()。包含(PPT)){
下载管理器mdDownloadManager =(下载管理器)MainActivity.this
.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request要求=新DownloadManager.Request(
Uri.parse(URL));
文件destinationFile =新的文件(Environment.getExternalStorageDirectory()的getFileName(URL));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setDescription(通过LearnPortal下载..);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationUri(Uri.fromFile(destinationFile));
mdDownloadManager.enqueue(请求);
值= FALSE;
}
}
如果(值){
view.loadUrl(URL);
}
}
返回值;
}
@覆盖
公共无效onPageFinished(的WebView视图,字符串URL){
super.onPageFinished(查看,网址);
}
/ **
*通知页面已经开始加载主机应用程序。
*
* @参数视图
* web视图开始回调。
* @参数网址
*该网页的网址。
* /
@覆盖
公共无效onPageStarted(的WebView视图,字符串URL,位图图标){
super.onPageStarted(查看,网址,图标);
}
}
/ **
*文件的URL名称
*
* @参数网址
* @返回
* /
公众的getFileName字符串(字符串URL){
串filenameWithoutExtension =;
filenameWithoutExtension =将String.valueOf(System.currentTimeMillis的());
返回filenameWithoutExtension;
}
}
HTTP: //developer.android.com/reference/android/webkit/DownloadListener.html
使用DownloadListener并覆盖onDownloadStart
实际下载逻辑检查onDownloadStartNoStream在下面的链接
<一个href=\"https://android.googlesource.com/platform/packages/apps/Browser/+/android-4.4_r1.1/src/com/android/browser/DownloadHandler.java\" rel=\"nofollow\">https://android.googlesource.com/platform/packages/apps/Browser/+/android-4.4_r1.1/src/com/android/browser/DownloadHandler.java
I'm working on an android application which uses a webview to open my site (url: learnportal.me) and then, upon entering the correct credentials, it will load a normal html page which has multiple links to download (those are all pdf/ppt/doc files).
All of this happens inside the webview.
All I want is the downloads to happen directly within the application itself, without any redirection to the default web browser in the mobile device.
Further more, I need the files to be downloaded to the SD card.
This is the MainActivity Page I'm currently working on... not sure if its correct though
public class MainActivity extends Activity {
@SuppressWarnings("deprecation")
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
webview.setWebChromeClient(new WebChromeClient());
WebViewClient client = new ChildBrowserClient();
webview.setWebViewClient(client);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
//webview.setInitialScale(1);
//webview.getSettings().setUseWideViewPort(true);
settings.setJavaScriptCanOpenWindowsAutomatically(false);
//settings.setBuiltInZoomControls(true);
settings.setPluginState(PluginState.ON);
settings.setDomStorageEnabled(true);
webview.loadUrl("http://learnportal.me");
//webview.setId(5);
//webview.setInitialScale(0);
//webview.requestFocus();
// webview.requestFocusFromTouch();
setContentView(webview);
}
/**
* The webview client receives notifications about appView
*/
public class ChildBrowserClient extends WebViewClient {
@SuppressLint({ "InlinedApi", "NewApi" })
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean value = true;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
String mimeType = mime.getMimeTypeFromExtension(extension);
if (mimeType != null) {
if (mimeType.toLowerCase().contains("pptx")
|| extension.toLowerCase().contains("pdf")
|| extension.toLowerCase().contains("doc")
|| extension.toLowerCase().contains("ppt")){
DownloadManager mdDownloadManager = (DownloadManager) MainActivity.this
.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
File destinationFile = new File(Environment.getExternalStorageDirectory(),getFileName(url));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setDescription("Downloading via LearnPortal..");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationUri(Uri.fromFile(destinationFile));
mdDownloadManager.enqueue(request);
value = false;
}
}
if (value) {
view.loadUrl(url);
}
}
return value;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
/**
* Notify the host application that a page has started loading.
*
* @param view
* The webview initiating the callback.
* @param url
* The url of the page.
*/
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
}
/**
* File name from URL
*
* @param url
* @return
*/
public String getFileName(String url) {
String filenameWithoutExtension = "";
filenameWithoutExtension = String.valueOf(System.currentTimeMillis());
return filenameWithoutExtension;
}
}
http://developer.android.com/reference/android/webkit/DownloadListener.html
use DownloadListener and override onDownloadStart
for the actual download logic check onDownloadStartNoStream in the below link
这篇关于如何使用下载管理器网页视图下载PDF / PPT / DOC文件到SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!