从应用程序下载文件后强制关闭错误

时间:2018-11-08 21:59:53

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

创建网络视图应用程序后,每当我从其中下载文件时,它都会下载并保存在本地存储中,并在栏中显示通知,但是当我单击该文件以从通知栏中打开它时,它将强制关闭该应用程序并关闭也有PDF阅读器

我的主要activity.java文件代码

ackage net.jptechsolutions.jptechinvoice;

import android.app.DownloadManager;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.CookieManager;
import android.webkit.DownloadListener;
import android.webkit.URLUtil;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

public WebView mywebview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mywebview=(WebView)findViewById(R.id.webview);
    WebSettings webSettings=mywebview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mywebview.loadUrl("https://*******"****""*n");

    mywebview.setWebViewClient(new WebViewClient());
    mywebview.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.setMimeType(mimeType);
            String cookies = CookieManager.getInstance().getCookie(url);
            request.addRequestHeader("cookie", cookies);
            request.addRequestHeader("User-Agent", userAgent);
            request.setDescription("Downloading file...");
            request.setTitle(URLUtil.guessFileName(url, contentDisposition,
                    mimeType));
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalPublicDir(
                    Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
                            url, contentDisposition, mimeType));
            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);
            Toast.makeText(getApplicationContext(), "Downloading File",
                    Toast.LENGTH_LONG).show();
        }});
}

@Override
public void onBackPressed() {
    if (mywebview.canGoBack())
    {
        mywebview.goBack();
    }
    else
    {
        super.onBackPressed();
    }

}
}

我该怎么办。

0 个答案:

没有答案