如何从sdcard打开pdf

时间:2019-04-30 07:02:17

标签: java android

尝试从设备上的下载文件夹中打开pdf文件, 我将文件名保存在firebase中,现在我要检查文件是否已在设备上找到,然后打开它,否则请从firebase下载。

File file = new File(Environment.getExternalStorageDirectory() + "filename");
System.out.println(Environment.getExternalStorageDirectory());

///but when i print Environment.getExternalStorageDirectory() i get 
///storage/emulated/0

if (file.exists()) {
    System.out.println(file.getAbsoluteFile());
    Intent target = new Intent(Intent.ACTION_VIEW);
    target.setDataAndType(Uri.fromFile(file), "application/pdf");
    target = Intent.createChooser(target, "Open File");
    target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    Intent intent = Intent.createChooser(target, "Open File");
    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        e.printStackTrace();
    }
} else {
    System.out.println("file not found");
}

1 个答案:

答案 0 :(得分:2)

尝试此操作以从下载目录获取文件

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + filename);