android - 当我通过意图打开 PDF 时,无法在 Android 中显示 PDF(无法打开)
问题描述
我正在通过 url 下载 PDF,保存后我通过意图打开它。但它说无法显示 PDF(无法打开)。我也授予了所有读写权限。PDF 保存成功,我也可以在我的文件管理器中看到,我可以从那里打开它。但不是来自我的应用程序。
使用的代码=>
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);
}
解决方案
如果该函数AppUtils.getTimeStamp()
确实在执行,那么预期您将在onDownloadComplete
. 尝试将文件名保存在局部变量中并重用它:
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));
}
});
}
推荐阅读
- android - 是否可以覆盖 WebRTC 连接超时?
- laravel - 我们可以在 laravel 的路由中传递 post 参数吗?
- rest - 如何在 JAX-RS 中读取从 Postman(表单数据)上传的文件?
- r - 如何根据数据框中的另一列计算一列的统计信息?
- javascript - 如何使用 JavaScript在链接的 HTMLCollection 中选择文本?
- react-native - 检查屏幕是否在 React Native 中变得模糊或聚焦?
- dataweave - Mule 4:Objectstore:无法反序列化对象
- php - PHP:shell_exec 拒绝 shell 命令的密码,但在 shell 中工作
- c# - 当 CPU 通过 IIS 高级 CPU 设置达到峰值时终止 W3WP
- python - 密集层:TypeError:init()缺少1个必需的位置参数:'units'