Android 是否从原始文件夹打开PDF文件?
如何从原始文件夹打开PDF文件? 此代码用于从SD卡访问PDF文件Android 是否从原始文件夹打开PDF文件?,android,pdf-reader,Android,Pdf Reader,如何从原始文件夹打开PDF文件? 此代码用于从SD卡访问PDF文件 File file = new File(getFilesDir(), "teen_ebook.pdf"); try { if (file.exists()) { Uri path = Uri.fromFile(file); Intent intent = new Intent(Intent.ACTION_VIEW); intent
File file = new File(getFilesDir(), "teen_ebook.pdf");
try {
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Log.e("IR", "No exception");
}
else {
Toast.makeText(PdfActivity.this, "File NotFound",Toast.LENGTH_SHORT).show();
}
}
catch (ActivityNotFoundException e) {
Toast.makeText(PdfActivity.this, "No Application Available to View PDF", Toast.LENGTH_SHORT).show();
}
但我想从原始文件夹访问PDF文件?
这里有人吗。请建议我。您不能直接从原始文件夹打开PDF文件。它需要PDFViewer应用程序才能打开PDF文件 您必须将PDF文件复制到SD卡中,然后才能打开阅读
public void openPDF(){
copyFile(getResources().openRawResource(R.raw.hello_world);
, new FileOutputStream(new File(getFilesDir(), "yourPath/teen_ebook.pdf")));
File pdfFile = new File(getFilesDir(), "yourPath/teen_ebook.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);
}
}
可能重复的请告诉我访问PDF文件的其他方式。