【发布时间】:2017-09-06 12:01:11
【问题描述】:
我进行了很多研究,但仍然没有找到在 Adobe 阅读器中打开下载的 pdf 的正确解决方案。 通过使用下面的代码,pdf 可以使用 google doc 应用程序打开,而不是 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 时显示吐司消息;
文件无法访问,请检查位置或网络连接。
【问题讨论】:
-
能否在 manifest、provider.xml 和 Utilities.isAboveNougat() 代码中发布 fileprovider 元数据
-
你能发布你的清单吗?
-
我已经编辑了我的问题。
-
@priyankakamthe 只是为了测试一次,您可以将 pdf 文件保存在特定文件夹中,例如 pdfs 并将该文件夹名称放在
试试? -
@Raghavendra 我试过但没用 :(
标签: android pdf adobe android-7.0-nougat android-fileprovider