【发布时间】:2014-08-20 08:39:18
【问题描述】:
我尝试在我的应用程序中打开 PDF。 首先,我这样创建 PDF:
String filename = Environment.getExternalStorageDirectory().toString()+"/mypdf.pdf";
File file = new File(filename);
try {
FileOutputStream bos = new FileOutputStream(file);
bos.write(Base64.decode(base64, 0));
bos.flush();
bos.close();
} catch (IOException e) {
Log.e(TAG, "IOError with PDF");
e.printStackTrace();
}
Intent intent = new Intent(this, PdfActivity.class);
intent.putExtra("file", filename);
startActivity(intent);
该文件创建良好且可读,我可以使用 ESExplorer 应用程序打开它。
该文件位于/storage/emulated/0/myfile.pdf
在 PdfActivity 中我尝试打开 PDF:
Bundle extras = getIntent().getExtras();
String url = extras.getString("file");
File file = new File(url);
try {
if (file.exists()) {
Uri path = Uri.parse(url);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
objIntent.setDataAndType(path, "application/pdf");
objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objIntent);
} else {
Toast.makeText(this, "File NotFound", Toast.LENGTH_SHORT).show();
}
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No Viewer Application Found", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
file.exists() return true,Intent 开始,但我的 PDF 阅读器说:“找不到文件”
我已经添加了对外部存储的读写权限。
有人知道为什么它无法访问我的文件吗?
【问题讨论】: