当我通过intent打开PDF时,无法在Android中显示PDF(无法打开)
我通过url下载PDF,保存后通过intent打开。但是它说不能显示PDF(不能打开)。我还授予了所有的读写权限。PDF已成功保存,我还可以在我的文件管理器中查看,并可以从那里打开它。但不是从我的应用程序 已用代码=>当我通过intent打开PDF时,无法在Android中显示PDF(无法打开),android,pdf,android-intent,downloadfile,Android,Pdf,Android Intent,Downloadfile,我通过url下载PDF,保存后通过intent打开。但是它说不能显示PDF(不能打开)。我还授予了所有的读写权限。PDF已成功保存,我还可以在我的文件管理器中查看,并可以从那里打开它。但不是从我的应用程序 已用代码=> private void downloadAndViewAttachment(String url) { String extension = url.substring(url.lastIndexOf(".")); Log.v(&quo
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 7/N以来,Uri.fromFile()不应该工作。除非您使用严格模式等恶劣行为。