无法从Android Webview中加载的网站下载该文件

时间:2015-12-04 10:19:52

标签: android android-intent android-activity webview

我正在研究一个在Webview中加载网站的简单应用程序。此应用程序仅包含webview,除此之外。其目的是将网页显示为应用程序。

当我的应用程序打开时,网站会自动加载,然后导航流程正常。

问题出现了,我的网页很少包含下载按钮。点击下载后,将下载格式为" .log,.txt,.pdf" 的文件。

当我在Android浏览器中加载chrome / Mozilla中的网站时,下载工作正常。但是当我在我的应用程序的Webview中加载网页时,下载无效。

我在调试模式下连接手机进行测试。在log-cat控制台中,我在Webview中的每次下载点击时看到以下错误

12-04 10:47:27.689 7142-7142/com.myweb.mweb W/cr.BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 7142

以下是MainActivity类

public class MYMainActivity extends AppCompatActivity {

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

        // clear webview db
        deleteDatabase("webview.db");
        deleteDatabase("webviewCache.db");

        // URL to load
        String url = "https://myweb.com";
        WebView myView = (WebView) findViewById(R.id.mywebview);

        // clear cache
        myView.clearCache(true);
        myView.clearHistory();
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookies(null);
        cookieManager.flush();

        WebSettings webSettings = myView.getSettings();
        webSettings.setJavaScriptEnabled(true); // add java script support for webview
        webSettings.setDomStorageEnabled(true);

        // to download the files
        myView.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, "Name of your downloadble file goes here, example: Mathematics II ");
                DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                dm.enqueue(request);
                Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); //This is important!
                intent.addCategory(Intent.CATEGORY_OPENABLE); //CATEGORY.OPENABLE
                intent.setType("*/*");//any application,any extension
                Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
                        Toast.LENGTH_LONG).show();
            }
        });

        // Load only SSL URL and only Authenticated URL
        WebViewClient MyWebViewClient = new WebViewClient()
        {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (Uri.parse(url).getHost().endsWith("myweb.com") &&
                        (Uri.parse(url).getScheme().equalsIgnoreCase("https")))  {
                    // This is a trusted site, so do not override; let my WebView load the page
                    return false;
                }
                // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
                // This is by default the Android Browser.
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            }
        };

        // set web client to not redirect and load in the same app opend
        myView.setWebViewClient(MyWebViewClient);
        //myView.setWebViewClient(new WebViewClient());
        myView.loadUrl(url);

    }
}

这是我的Manifest .xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myweb.mweb">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/my_logo"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MYMainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar"
            android:configChanges="orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

为什么我无法通过webview下载文件..伙计们帮帮我

0 个答案:

没有答案