1
由於谷歌驅動器無法下載超過25 MB的文件,因爲它要求從HTML頁面授權用戶,所以我認爲有必要讓這些文件在webView中顯示「病毒警告」頁面我的應用程序所以我在Android studio中創建了這個小樣本進行測試。這是很簡單的一個MainActivity和包含一個簡單的WebView的activity.xml:如何使用WebView和WebClient for Android管理「病毒警告」警報頁面?
public class MainActivity extends AppCompatActivity {
private static final int WRITE_SDCARD_REQUEST_CODE = 12;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// permission to write file on internal storage ....
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
WRITE_SDCARD_REQUEST_CODE);
} else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
WRITE_SDCARD_REQUEST_CODE);
}
}
else {
// launch my webPage and webClient withe the given url of my file
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("https://drive.google.com/uc?export=download&id=0B4fwFC8FCSQGGTZfbHYwczBXVjg");
myWebView.setWebViewClient(new MyWebViewClient());
}
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// return after valid with "Download anyway " button
Log.i("URL",url);//<-- url result: https://drive.google.com/uc?export=download&confirm=LoXi&id=0B4fwFC8FCSQGGTZfbHYwczBXVjg
//with "confirm=LoXi" Added to the original link
// so i try to download this new confirmed url given by Google Drive warning page
// But the link is not valid for download!!!!! Why????
DownloadFileFromURL dFURL = new DownloadFileFromURL();
dFURL.execute(url);
// I obtain a wrong file .... 36 Ko...not good!
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == WRITE_SDCARD_REQUEST_CODE)
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
WebView myWebView = (WebView) findViewById(R.id.webview);
// original link of file Google drive that i want to download
myWebView.loadUrl("https://drive.google.com/uc?export=download&id=0B4fwFC8FCSQXbTZfbHYwczBXVjg");
myWebView.setWebViewClient(new MyWebViewClient());
}
}
class DownloadFileFromURL extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100%
// progress bar
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream(),
8192);
// Output stream
OutputStream output = new FileOutputStream(Environment
.getExternalStorageDirectory().toString()
+ "/video.mp4");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress("" + (int) ((total * 100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
//pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
//dismissDialog(progress_bar_type);
}
}
}
從我的超過25 MB(在這裏90莫)我也顯示頁面文件的下載鏈接驅動器的「病毒警告」。點擊下載按鈕「無論如何下載」後,我可以在我的WebClient中獲得修改的鏈接。但是這個鏈接似乎並不正確。如果我嘗試複製/粘貼它在鉻我回到「病毒警告」的同一頁!爲什麼??
當我右鍵單擊「下載任何」按鈕來複制鏈接時,該鏈接與我通過WebClient獲得的形式相同,但是「confirm = XXXX」部分不是,並且它可以訪問下載!
你能給我你的意見和建議嗎?
謝謝。
如果我想在雲端硬盤中下載文件,我通常會使用webContentLink,它可以通過成功調用[Files:get]獲得(https://developers.google.com/drive/v3/reference/files /得到)。我在瀏覽器上運行鏈接,並最終爲我下載文件。我沒有深入Android開發,但我建議使用WebView運行webContentLink。 – noogui
@noogui所以你可以下載一個文件> 25MB與webContentLink沒有問題?你確定? – Aristide13
如果超過25MB,警告將始終顯示。我只是說在獲得webContentLink後,通過WebView運行它。如果警告顯示「仍然繼續」。 – noogui