如何查看PDF文件

时间:2017-01-13 05:46:22

标签: java android file

我无法用我的程序读取pdf文件。代码很简单,但遗憾的是它不起作用。提前谢谢你

    String path =getActivity().getFilesDir()+"/test.pdf";
    File file = new File(path);
    Intent target = new Intent(Intent.ACTION_VIEW);
    target.setDataAndType(Uri.fromFile(file),"application/pdf");
    target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    Intent intent=Intent.createChooser(target,"Open File");
    startActivity(intent);

3 个答案:

答案 0 :(得分:0)

以下是有关如何使用Intent选择器打开该文件(test.pdf)的代码:

File file = new File(getActivity().getFilesDir()+"/test.pdf");
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Intent intent = Intent.createChooser(target, "Open File");
try {
    startActivity(intent);
} catch (ActivityNotFoundException e) {
    // Instruct the user to install a PDF reader here, or something
}

Source

答案 1 :(得分:0)

Android 5.0

中添加了

PdfRenderer支持

在Android 5.0之下,您必须制定解决方法

请参阅CommonsWare撰写的此博客,以查看PDF

答案 2 :(得分:0)

如何打开pdf文件click here for complete code

 private void openRenderer(String filePath) {
            File file = new File(filePath);
            try {
                mFileDescriptor = ParcelFileDescriptor.open(file,
                        ParcelFileDescriptor.MODE_READ_ONLY);
                mPdfRenderer = new PdfRenderer(mFileDescriptor);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }