如何从Web视图下载带有扩展名的APK文件?

时间:2015-10-11 22:52:33

标签: javascript java android webview

我创建了一个带有网络视图的应用。此Web视图打开一个页面,其中包含许多下载APK的链接。下载过程顺利进行,但下载的apk文件没有得到其原始名称和扩展名。

这是我的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tech_news);

    techView = (WebView) findViewById(R.id.tech_webView);
    techView.getSettings().setJavaScriptEnabled(true);
    techView.loadUrl("http://egy-tech-droid.blogspot.com.eg/2015/08/blog-post.html");

    // This will handle downloading. It requires Gingerbread, though
    final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

    // This is where downloaded files will be written, using the package
    // name isn't required
    // but it's a good way to communicate who owns the directory
    final File destinationDir = new File(Environment.getExternalStorageDirectory(), getPackageName());
    if (!destinationDir.exists()) {
        destinationDir.mkdir(); // Don't forget to make the directory if
                                // it's not there
    }

    techView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);

            techView.setDownloadListener(new DownloadListener() {
                public void onDownloadStart(String url, String userAgent, String contentDisposition,
                        String mimetype, long contentLength) {
                    Request request = new Request(Uri.parse(url));
                    request.allowScanningByMediaScanner();
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, contentDisposition);
                    DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                    dm.enqueue(request);



                    Toast.makeText(getApplicationContext(), "downloading",
                            // To notify the user that the file
                            // is being downloaded.
                            Toast.LENGTH_LONG).show();

                }
            });

            return true;
        }
    });

}

我希望下载的文件获取其原始名称和扩展名,因此当用户稍后从文件资源管理器中打开文件时,系统会将该文件识别为apk,而不仅仅是未知文件。

0 个答案:

没有答案