如何在不使用任何外部应用程序的情况下从webview下载文件到内部存储位置(internalstorage-> downloads)?

时间:2017-04-16 12:53:33

标签: android webview android-webview android-download-manager

我尝试了很多解决方案但无法在内部存储中写入。

我已经包含了权限 INTERNET,WRITE_EXTERNAL_STORAGE,WRITE_INTERNAL_STORAGE,ACCESS_DOWNLOAD_MANAGER

我的网页浏览主要遇到.pdf,.doc。,。ppt文件类型。

每当我点击网页视图中的文件时,必须允许我从内部存储中选择位置并开始下载过程。

@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);
}
}

0 个答案:

没有答案