【问题标题】:How to pick a file from gallery?如何从图库中选择文件?
【发布时间】:2014-03-19 12:52:01
【问题描述】:

我想选择一个文件(图片、视频或音频除外),例如 pdf、ppt、docx、txt 等,

Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select File"),Constant.SELECT_FILE);

以上方法无效。

当我执行该操作时,会出现对话框,并显示消息“没有应用程序可以执行此操作”

【问题讨论】:

标签: android android-gallery


【解决方案1】:

因为设备上没有安装任何应用程序,可以通过Intent.ACTION_GET_CONTENT 操作来处理您的意图。因此,如果您的设备在 Android 4.4 上运行,请尝试使用 Intent.ACTION_OPEN_DOCUMENT 或实现您自己的文件浏览器活动。

看看https://github.com/vaal12/AndroidFileBrowseraFileChooser

【讨论】:

    【解决方案2】:

    试试这个:

    问题是没有安装应用程序来处理打开 PDF。您应该使用 Intent Chooser,如下所示:

    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
    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
    }   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-30
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多