java - 如何从SD卡打开pdf

标签 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");
}

最佳答案

尝试从下载目录获取文件

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

关于java - 如何从SD卡打开pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55915507/

相关文章:

Java Web 服务 tomcat 无法找到共享库

Java move 具有特定文件扩展名的文件

java - BulkProcessor 和 BulkRequestBuilder - elasticSearch (java) 之间有什么区别?

java - Android 搜索字符串和 "Trim"

java - 如何解析 android Activity_main.xml 文件并修改它?

android - 如何将 JSONObject 转换为类对象?

java - ORA-01005 空密码 : What does Oracle consider to be null?

java - Java中存储多个线程

java - 仅单击的项目可见,并隐藏 RecyclerView 中之前的项目

android - 如何在我的 Android 应用程序中使用 Twilio 发送短信?