Android 我想让我的应用打开pdf
我的应用程序从一个链接下载了一个pdf文件,并将其保存到SD卡上,它工作正常。。但我一直在苦苦挣扎的是,我不能让我的应用程序查看pdf文件..(尽管我已经在模拟器上安装了pdf viewer),下载文件后什么都没有发生。。。 这是我打开pdf文件的代码Android 我想让我的应用打开pdf,android,file,pdf,Android,File,Pdf,我的应用程序从一个链接下载了一个pdf文件,并将其保存到SD卡上,它工作正常。。但我一直在苦苦挣扎的是,我不能让我的应用程序查看pdf文件..(尽管我已经在模拟器上安装了pdf viewer),下载文件后什么都没有发生。。。 这是我打开pdf文件的代码 InputStream input = new BufferedInputStream(url.openStream()); File sdCard =
InputStream input = new BufferedInputStream(url.openStream());
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath()
+ "/myapp/download");
dir.mkdirs();
File file = new File(dir, "application/pdf");
FileOutputStream output = new FileOutputStream(file);
使用以下代码打开pdf文件
File pdfFile = new File("/mnt/sdcard/path_to_file/yourpdf.pdf");
if(pdfFile.exists()) {
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(pdfIntent);
} catch(ActivityNotFoundException e) {
Toast.makeText(yourActivityClass.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(yourActivityClass.this, "File not found", Toast.LENGTH_LONG).show();
}
yourActivityClass。这是应用程序上下文,请在测试上述代码时进行更正。请在发布前搜索。和