Search code examples
androidmime-typesstorage-access-framework

Using mimetype 'application/octet-stream' with SAF picker works for local files but not for the same files in Google Drive


I'm refactoring my backup/restore feature to work with Storage Access Framework. My backups are zip files with custom extensions (.dtt). If I try to open the picker with the following code, my .dtt files are selectable from the local Downloads directory but not from a directory in Google Drive. How can I make the file selectable even from Google Drive?

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);

Notice how the backup file is selectable from the local "Downloads" directory, but grayed out in the Google Drive "backups" directory:

local Downloads directory

Google Drive directory

I know this behavior can be achieved, because NovaLauncher's restore feature behaves correctly.


Solution

  • I found a working solution. It looks like Google Drive changes the mime type of my files to "application/x-zip" upon uploading them. So I need to account for both mime types in order to make my files selectable in both places:

    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);
    

    If there's a better solution, or some sort of problem with this one, I'd love to hear about it.