在Android中显示PDF - 大白社区
- 发帖时间:
- 2021-06-05 08:30:14
摘要:在我的onCreate()中我做了这个检查://// check if we have a PDF viewer,else bad things happen//Intent intent = new Intent...intent.setType(\"application/pdf\");List intents = getPackageManager().que...
在我的onCreate()中我做了这个检查:
//
// check if we have a PDF viewer,else bad things happen
//
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("application/pdf");
List intents = getPackageManager().queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY);
if (intents == null || intents.size() == 0) {
// display message then...
finish();
}
在我的HTC Desire上,即使我有Adobe的PDF查看器,也不会返回匹配项.这个问题的答案android: open a pdf from my app using the built in pdf viewer提到Adobe可能没有任何公共意图,因此上述检查显然不会返回任何内容.
任何人都可以验证您是否应该从意图启动Acrobat,或者是否有其他方法或PDF查看器可供使用.
实际用例是下载发票副本并使用以下代码将其存储在本地存储中:
URL url = new URL(data);
InputStream myInput = url.openConnection().getInputStream();
FileOutputStream fos = openFileOutput(fname,Context.MODE_WORLD_READABLE);
// transfer bytes from the input file to the output file
byte[] buffer = new byte[8192];
int length;
while ((length = myInput.read(buffer)) > 0) {
fos.write(buffer,length);
progressDialog.setProgress(i++);
}
fos.close();
然后展示
// read from disk,and call intent
openFileInput(fname); // will throw FileNotFoundException
File dir = getFilesDir(); // where files are stored
File file = new File(dir,fname); // new file with our name
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.fromFile(file));
intent.setType("application/pdf");
startActivity(intent);