我有很多研究,但我仍然无法在Adobe阅读器中打开下载的pdf。 通过使用下面的代码,pdf可以使用谷歌文档应用程序打开,但不能通过adobe打开。
从Url下载pdf
URL url = new URL(f_url[0]);
try {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + getFileName());
byte[] buffer = new byte[AccessibilityNodeInfoCompat.ACTION_DISMISS];
while (true) {
int bufferLength = inputStream.read(buffer);
if (bufferLength <= 0) {
break;
}
fileOutputStream.write(buffer, 0, bufferLength);
}
fileOutputStream.close();
} catch (Exception e) {
FileLog.e("PDF Save", e);
}
查看已保存的PDF
File file = new File(Environment.getExternalStorageDirectory() + "/" + getFileName());
try {
if (file.exists()) {
Uri fileUri = Uri.fromFile(file);
if (Utilities.isAboveNougat()) {
Uri contentUri = FileProvider.getUriForFile(mContext, "myPackage.fileprovider", file);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(contentUri, "application/pdf");
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
mContext.startActivity(intent);
} else {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri, "application/pdf");
mContext.startActivity(intent);
}
} else {
Toast.makeText(mContext, "Not able to find downloaded file", Toast.LENGTH_LONG).show();
}
} catch (ActivityNotFoundException ae) {
Toast.makeText(mContext, "No activity found to open this attachment.", Toast.LENGTH_LONG).show();
}
public static boolean isAboveNougat() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return true;
}
return false;
}
的Manifest.xml;
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="myPackage.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="." name="external_storage_root" />
</paths>
我也试过以下方式打开pdf;
Intent intent = ShareCompat.IntentBuilder.from((Activity) mContext)
.setStream(contentUri)
.setType("application/pdf")
.getIntent()
.setAction(Intent.ACTION_VIEW)
.setDataAndType(contentUri, "application/pdf") .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
mContext.startActivity(intent);
但仍然无法解决问题。在使用adobe打开pdf时显示toast消息;
文件无法访问,请检查位置或网络连接。
答案 0 :(得分:1)
最后我才知道我犯了错误。从Url下载pdf时,它以字节为单位获得存储。 adobe无法读取该字节格式的文件。因此,在保存下载的文件时,我们需要提供 .pdf 扩展名,这是我在存储时没有给出的。查看应用程序的大多数文档都能够读取字节格式的文件,例如google的docs app。
我在getFileName函数中进行了更改;
private String getFileName() {
String tempFileName = urlString.substring(urlString.length() - 10);
String[] arrFilenames = tempFileName.split("\\.");
return Utilities.getString(arrFilenames[0]+".pdf");
}
答案 1 :(得分:0)
Uri contentUri = FileProvider.getUriForFile(mContext, "myPackage.fileprovider", file);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(contentUri, "application/pdf");
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
mContext.startActivity(intent);
对于nougat这对我有用