我创建了一个类openPDF,它接受一个字节数组作为输入,并使用Adobe Reader显示该文件。代码:
private void openPDF(byte[] PDFByteArray) {
try {
// create temp file that will hold byte array
File tempPDF = File.createTempFile("temp", ".pdf", getCacheDir());
tempPDF.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempPDF);
fos.write(PDFByteArray);
fos.close();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(tempPDF);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} catch (IOException ex) {
String s = ex.toString();
ex.printStackTrace();
}
}当我传递意图时,来自adobe阅读器的错误是“无效的文件路径”。我阅读了所有其他与在android中下载和查看PDF相关的帖子,但帮助不大。有什么建议吗?
发布于 2012-09-26 21:14:00
我写这段代码是为了用Adobe的应用程序打开Dowloads文件夹中的一个特定的.pdf文件
File folder = new File(Environment.getExternalStorageDirectory(), "Download");
File pdf = new File(folder, "Test.pdf");
Uri uri = Uri.fromFile(pdf);
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.adobe.reader");
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);这对我很管用。所以我猜你的问题可能是临时文件。尝试将文件写入sdcard。为此,您需要在AndroidManifest.xml中添加android.permission.WRITE_EXTERNAL_STORAGE。
发布于 2012-08-03 16:52:39
我认为问题在于其他应用程序无法访问您应用程序的私有数据区(如缓存目录)中的文件。
候选解决方案:
..。字符串fn = "temp.pdf";上下文c= v.getContext();FileOutputStream fos = null;try { fos = c.openFileOutput(fn,Context.MODE_WORLD_READABLE);fos.write(PDFByteArray);} catch (FileNotFoundException e) { //做某事} catch (IOException e) { // TODO自动生成的catch块e.printStackTrace();}最终{ if (fos!=null) { try { fos.close();} catch (IOException e) { // TODO自动生成的捕获块e.printStackTrace();} intent = new Intent();intent.setAction(Intent.ACTION_VIEW);字符串filename = c.getFilesDir() + File.separator + fn;File file = new file(文件名);Uri uri =Uri.fromFile(文件);intent.setDataAndType(uri,“应用程序/pdf”);intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);startActivity(intent);...
您可以使用android.os.Environment接口获取路径,记得将权限添加到您的应用的AndroidManifest.xml文件中。
问候
陈子腾
https://stackoverflow.com/questions/11790152
复制相似问题