由网友(傲慢与你)分享简介:我写打开一个PDF文件,当你点击一个按钮的应用程序。下面是我的code:I am writing an application that opens a pdf file when you click a button. Below is my code:File pdfFile = new File("andro...
我写打开一个PDF文件,当你点击一个按钮的应用程序。下面是我的code:
I am writing an application that opens a pdf file when you click a button. Below is my code:
File pdfFile = new File(
"android.resource://com.dave.pdfviewer/"
+ R.raw.userguide);
Uri path = Uri.fromFile(pdfFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/pdf");
startActivity(intent);
然而,当我运行它,preSS按钮,它说:该文件无法打开,因为它不是一个有效的PDF文件。这是推动我疯了。我是否正确地访问文件?有任何想法吗?谢谢
However when I run it and press the button it says "The document cannot be opened because its is not a valid PDF document". This is driving me mad. Am I accessing the file correctly? Any ideas? Thanks
推荐答案
您必须从资产的文件夹中的PDF格式复制到SD卡的文件夹。
You have to copy the pdf from assets folder to sdcard folder.
.....
copyFile(this.getAssets().open("userguide.pdf"), new FileOutputStream(new File(getFilesDir(), "yourPath/userguide.pdf")));
File pdfFile = new File(getFilesDir(), "yourPath/userguide.pdf"); Uri path = Uri.fromFile(pdfFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/pdf");
startActivity(intent);
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
相关推荐
最新文章