【问题标题】:Cannot set a fileIntent for image files of type jpg, png and jpeg无法为 jpg、png 和 jpeg 类型的图像文件设置 fileIntent
【发布时间】:2020-03-22 18:11:48
【问题描述】:

我正在使用以下代码:

Intent fileIntent = new Intent(Intent.ACTION_GET_CONTENT);
fileIntent.setType("image/jpg|image/png|image/jpeg"); 
startActivityForResult(fileIntent, REQUEST_FILE);

我相信代码应该允许我选择任何 jpg、png 或 jpeg 文件,但它没有。

另一方面,改变 fileIntent.setType("image/jpg|image/png|image/jpeg");到 fileIntent.setType("image/*");允许选择文件。但它也允许选择我不想选择的其他图像文件。

fileIntent.setType("image/jpg|image/png|image/jpeg");所以它允许选择那些类型的文件而不是其他类型的文件?

【问题讨论】:

    标签: android image filechooser


    【解决方案1】:

    您只能使用setType() 指定单个mimetype,而不是使用带有mimetypes 数组的键Intent.EXTRA_MIME_TYPES 应用额外的:

    fileIntent.putExtra(Intent.EXTRA_MIME_TYPES, Arrays.asList("image/png", "image/jpeg").toArray());
    

    我发现,即使在提供 mimetype 列表时,您仍可能需要将 intent.fileType("*/*"); 应用于意图,但是,mimetype 列表将优先考虑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-07
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      相关资源
      最近更新 更多