Android 如何在不使用任何外部应用程序的情况下从webview下载文件到我的内部存储位置(internalstorage->;downloads)?
我尝试了许多解决方案,但无法在内部存储器中写入 我已经包括了像这样的权限 INTERNET、写入外部存储、写入内部存储、访问下载管理器 我的webview主要遇到.pdf、.doc、.ppt类型的文件 每当我在web视图中单击文件时,它必须允许我从内部存储器中选择位置并开始下载过程Android 如何在不使用任何外部应用程序的情况下从webview下载文件到我的内部存储位置(internalstorage->;downloads)?,android,webview,android-webview,android-download-manager,Android,Webview,Android Webview,Android Download Manager,我尝试了许多解决方案,但无法在内部存储器中写入 我已经包括了像这样的权限 INTERNET、写入外部存储、写入内部存储、访问下载管理器 我的webview主要遇到.pdf、.doc、.ppt类型的文件 每当我在web视图中单击文件时,它必须允许我从内部存储器中选择位置并开始下载过程 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); s
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/* FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});*/
wv=(WebView)findViewById(R.id.wv);
WebSettings ws=wv.getSettings();
ws.setJavaScriptEnabled(true);
ws.setBuiltInZoomControls(true);
ws.setSupportZoom(true);
ws.setUseWideViewPort(true);
ws.setJavaScriptCanOpenWindowsAutomatically(true);
wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
wv.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
wv.getSettings().setAppCacheEnabled(true);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
ws.setDomStorageEnabled(true);
ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
ws.setUseWideViewPort(true);
ws.setSavePassword(true);
ws.setEnableSmoothTransition(true);
ws.setSaveFormData(true);
wv.loadUrl("http://courses.rvrjcce.ac.in/moodle/login/index.php");
wv.setWebViewClient(new MyWebviewClient());
wv.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.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "ganesh");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
}
});
}
private class MyWebviewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return false;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(wv.canGoBack())
{
wv.goBack();
}
else
{
Intent inte=new Intent(MainActivity.class.getName());
startActivity(inte);
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
让您的
DownloadListener
直接使用HttpURLConnection
、OkHttp等进行下载。您能简要介绍一下吗