Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 469
Gavin Wright Avatar asked Nov 30 '25 02:11

Gavin Wright


1 Answers

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.

like image 121
Gavin Wright Avatar answered Dec 02 '25 17:12

Gavin Wright



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!