将模仿类型'application / octet-stream'与SAF选择器一起使用适用于本地文件,但不适用于Google云端硬盘中的相同文件

问题描述 投票:2回答:1

我正在重构备份/还原功能以与Storage Access Framework一起使用。我的备份是带有自定义扩展名(.dtt)的zip文件。如果我尝试使用以下代码打开选择器,则可以从本地“下载”目录中选择我的.dtt文件,但不能从Google云端硬盘中的目录中选择。即使在Google云端硬盘中,如何使文件可供选择?

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/octet-stream");
startActivityForResult(intent, OPEN_SAF_PICKER_REQUEST_CODE);

注意如何从本地“下载”目录中选择备份文件,但在Google云端硬盘“ backups”目录中显示为灰色:

local Downloads directory

Google Drive directory

我知道此行为可以实现,因为NovaLauncher的还原功能行为正确。

android mime-types storage-access-framework
1个回答
0
投票

我找到了可行的解决方案。看起来Google云端硬盘在上传文件时将其MIME类型更改为“ application / x-zip”。所以我需要考虑两种mime类型:

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
String[] mimeTypes = {"application/octet-stream","application/x-zip"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
startActivityForResult(intent, OPEN_SAF_PICKER_REQUEST_CODE);

如果有更好的解决方案,或者对此有某种问题,我很乐意听到。

© www.soinside.com 2019 - 2024. All rights reserved.