[英]How To Download PDF File From Google Drive with WebView
I want to download a file from my drive link.我想从我的驱动器链接下载文件。 I don't know how to do it and what's the code.
我不知道该怎么做,代码是什么。 The URL is https://drive.google.com/open?id=1txJFXxL9uY3AjyQ3C2G9t9lSkhJqaxuO .
URL 是https://drive.google.com/open?id=1txJFXxL9uY3AjyQ3C2G9t9lSkhJqaxuO 。 When I try this code, my WebView go freezing.
当我尝试这段代码时,我的 WebView 冻结了。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wvnpwpop);
//Webview
WebView webView = findViewById(R.id.wvnpwpop);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://drive.google.com/open?id=1txJFXxL9uY3AjyQ3C2G9t9lSkhJqaxuO");
webView.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);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "myPDFfile.pdf");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
//WebSettings
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportMultipleWindows(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
// -------- End Of WebView and Web Settings
Try this and check:试试这个并检查:
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(mUrl));
startActivity(i);
}
});
You may need to login.您可能需要登录。 Find url to login and redirect to your gdrive distination
查找 url 登录并重定向到您的 gdrive distination
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.