简体   繁体   English

当我通过 Intent 打开 PDF 时,无法在 Android 中显示 PDF(无法打开)

[英]Cannot display PDF (cannot be open) in Android when I open PDF through intent

I am downloading PDF through url and after saving I am opening it through intent.我正在通过 url 下载 PDF,保存后我通过意图打开它。 But it says Cannot display PDF (cannot be open).但它说无法显示PDF(无法打开)。 I've granted all read and write permission also.我也授予了所有读写权限。 PDF is saving successfully and I can also see in my File Manager and I can open it from there. PDF 保存成功,我也可以在我的文件管理器中看到,我可以从那里打开它。 But not from my app.但不是来自我的应用程序。

Used code=>使用的代码=>

  private void downloadAndViewAttachment(String url) {

    String extension = url.substring(url.lastIndexOf("."));
    Log.v("extension", extension);

    fileName = url.substring(url.lastIndexOf("/"));
    fileName = fileName.replace("/", "");
    Log.v("fileName", fileName);
    final File file = new File(Environment.getExternalStoragePublicDirectory(getString(R.string.app_name)),   AppConstants.Attachments);

    AndroidNetworking.download(url, file.getPath(), AppUtils.getTimeStamp() + "_" + fileName)
            .setPriority(Priority.HIGH)
            .build()
            .setDownloadProgressListener(new DownloadProgressListener() {
                @Override
                public void onProgress(long bytesDownloaded, long totalBytes) {

                    AppUtils.showRequestDialog(mActivity);

                }
            })
            .startDownload(new DownloadListener() {
                @Override
                public void onDownloadComplete() {

                    AppUtils.hideDialog(mActivity);
                    AppUtils.showToastSort(mActivity, getString(R.string.download_succesfully));
                    openPdfFile(file.getPath() + "/" + AppUtils.getTimeStamp() + "_" + fileName);
                  

                }

                @Override
                public void onError(ANError anError) {

                    AppUtils.hideDialog(mActivity);
                    Log.v("downloadError", anError.getErrorBody());
                    Log.v("downloadError", anError.getErrorDetail());
                    Log.v("downloadError", String.valueOf(anError.getErrorCode()));
                    AppUtils.showToastSort(mActivity, getString(R.string.something_error));

                }
            });

}

private void openPdfFile(String path) {

    AppUtils.hideDialog(mActivity);

    File file = new File(path);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file), "application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(intent);

}

If the function AppUtils.getTimeStamp() is really doing, what is expected you'll get a different timestamp in onDownloadComplete .如果AppUtils.getTimeStamp()函数确实在执行,那么您将在onDownloadComplete获得不同的时间戳。 Try to save the filename in a local variable and reuse it:尝试将文件名保存在局部变量中并重用它:

private void downloadAndViewAttachment(String url) {

String extension = url.substring(url.lastIndexOf("."));
Log.v("extension", extension);

fileName = url.substring(url.lastIndexOf("/"));
fileName = fileName.replace("/", "");
Log.v("fileName", fileName);
final File file = new File(Environment.getExternalStoragePublicDirectory(getString(R.string.app_name)),   AppConstants.Attachments);
// Save complete Filename in variable.
final String fullFilename = AppUtils.getTimeStamp() + "_" + fileName;
AndroidNetworking.download(url, file.getPath(), fullFilename)
        .setPriority(Priority.HIGH)
        .build()
        .setDownloadProgressListener(new DownloadProgressListener() {
            @Override
            public void onProgress(long bytesDownloaded, long totalBytes) {

                AppUtils.showRequestDialog(mActivity);

            }
        })
        .startDownload(new DownloadListener() {
            @Override
            public void onDownloadComplete() {

                AppUtils.hideDialog(mActivity);
                AppUtils.showToastSort(mActivity, getString(R.string.download_succesfully));
                // Reuse variabel.
                openPdfFile(file.getPath() + "/" + fullFilename);
              

            }

            @Override
            public void onError(ANError anError) {

                AppUtils.hideDialog(mActivity);
                Log.v("downloadError", anError.getErrorBody());
                Log.v("downloadError", anError.getErrorDetail());
                Log.v("downloadError", String.valueOf(anError.getErrorCode()));
                AppUtils.showToastSort(mActivity, getString(R.string.something_error));

            }
        });

}

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

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