简体   繁体   English

如何从WebView下载文件并从WebView打开PlayStore

[英]How to download files from a webview and open playstore from webview

I have searched other questions for how to download files from a WebView . 我搜索了其他问题,以了解如何从WebView下载文件。

I want to be able to download files using website and also allow a user to open the Playstore from the WebView . 我希望能够使用网站下载文件,并且还允许用户从WebView打开Playstore。 For example, some websites have a link or an ad from their app that opens up the dialogue box to open with Playstore or browser. 例如,某些网站从其应用程序中获得一个链接或一个广告,该链接或广告会打开对话框以使用Playstore或浏览器打开。 How do I go about doing that? 我该怎么做?

Below is the code I have: 下面是我的代码:

import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.app.Activity;
import android.webkit.DownloadListener;
import android.app.DownloadManager.Request;
import android.app.DownloadManager;
import android.os.Environment;
import android.net.Uri;

public class MainActivity extends Activity {
    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Allow third party cookies for Android Lollipop
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mWebView = (WebView) findViewById(R.id.activity_main_webview);
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.setAcceptThirdPartyCookies(mWebView,true);
        }

        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.getSettings().setGeolocationEnabled(true);
        mWebView.setWebChromeClient(new WebChromeClient() {
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                // callback.invoke(String origin, boolean allow, boolean remember);
                callback.invoke(origin, true, false);
            }
        });

        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setAppCacheEnabled(true);
        mWebView.getSettings().setDatabaseEnabled(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setAllowFileAccess(true);
        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mWebView.getSettings().setSupportMultipleWindows(true);
        String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
        mWebView.getSettings().setAllowFileAccess(true);
        mWebView.getSettings().setAppCachePath(appCachePath);

        mWebView.loadUrl("http://www.mywebsite.com/");
        mWebView.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                //for downloading directly through download manager
                Request request = new Request(Uri.parse(url));
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download.mp3");
                DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                dm.enqueue(request);
            }
        });
    }
}

EDIT: Forgot to mention that it does work but after the file downloads, it says "Cannot open file" 编辑:忘记提及它确实有效,但是在文件下载后,它说“无法打开文件”

What logs related to the "Cannot open file" do you have? 您有哪些与“无法打开文件”相关的日志? please give more info related to this.... 请提供与此有关的更多信息。

As a first approach I recommend to add the missing: 作为第一种方法,我建议添加缺少的内容:

    request.setMimeType(mimeType);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM