【问题标题】:How to Open Pdf url without webview in android studio如何在android studio中打开没有webview的Pdf url
【发布时间】:2018-10-25 20:29:17
【问题描述】:

请告诉我任何知道如何在没有 webview 的情况下在 android 中打开 pdf url 或知道最佳实践或库的人

pdf url=http://www3.nd.edu/~cpoellab/teaching/cse40816/android_tutorial.pdf示例

【问题讨论】:

  • 下载PDF,然后使用any of these PDF viewing solutions
  • 你知道如何将我们的下载路径指向应用程序的目录资产吗?
  • 资产是只读的。资产位于开发机器上的目录中,但它们只是设备上 APK 中的条目。
  • 如果你想在你的应用中展示它,你可以使用这个github.com/barteksc/AndroidPdfViewer

标签: android android-studio android-studio-3.0 android-studio-2.3


【解决方案1】:

方法有:

强制下载并打开它:

 webView.setDownloadListener(new DownloadListener() {
        @Override
        public void onDownloadStart(String aUrl, String userAgent, String contentDisposition, String mimetype, long contentLength) {

            fileName = URLUtil.guessFileName(aUrl, contentDisposition, mimetype); //returns a string of the name of the file THE IMPORTANT PART

                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(aUrl));
                    request.allowScanningByMediaScanner();
                    CookieManager cookieManager = CookieManager.getInstance();
                    String cookie = cookieManager.getCookie(aUrl);
                    request.addRequestHeader("Cookie", cookie);
                    request.setMimeType("application/pdf");
                    request.addRequestHeader("User-Agent", userAgent);
                    Environment.getExternalStorageDirectory();
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
                    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                    dm.enqueue(request);
                    registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));





        }


    });

下载后不要忘记添加打开pdf的方法:

BroadcastReceiver onComplete=new BroadcastReceiver() {
        public void onReceive(Context ctxt, Intent intent) {
            openFile(fileName);        }
    };

    protected void openFile(String fileName) {
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + File.separator +
                fileName);
        Uri path = Uri.fromFile(file);
        Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
        pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        pdfOpenintent.setDataAndType(path, "application/pdf");
        try {
            this.startActivity(pdfOpenintent);
        } catch (ActivityNotFoundException e) {
        }
    }

也不要忘记在 :

中声明权限
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

或者你只能

有意打开

webView.setDownloadListener(new DownloadListener() {
    public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    }
});

两种解决方案都经过测试并且有效。

【讨论】:

  • 如何将下载文件保存在我的文件夹应用程序中,比如文件夹资产?
  • 您可以将其保存在应用程序之外并通过外部存储访问获取它
  • 我想把它保存在应用程序中,你有建议代码吗?或示例应用程序?
猜你喜欢
  • 2018-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多