I have a feature of import and export DB file in my app. I have used mime-type application/x-sqlite3
and it was working fine in earlier versions. However, in Android 11 export is working as expected but when opt for import, DB file that was copied is grayed out. Therefore, I can not select the file for further processing. If I use intent.setType("*/*");
, I can access the file but I do not want to make every file selectable. I wanted to filter out only sqlite DB file.
I also tried with "application/vnd.sqlite3", "application/octet-stream"
but the scenario persists.
Here is the code snippet:
private void handleImportView() {
Intent intent = new Intent();
String[] mimetypes = {"application/x-sqlite3","application/vnd.sqlite3", "application/octet-stream"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select file to import"), REQUEST_CODE_RESTORE);
}
This is now solved.
I was creating a file to export using following code:
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/x-sqlite3");
intent.putExtra(Intent.EXTRA_TITLE, filename);
startActivityForResult(intent, REQUEST_CODE_FILE_CREATED);
Although it is specified "application/x-sqlite3"
as a mime type, It actually created the file using mime application/x-trash
.
I have used intent.setType("*/*");
and getContentResolver().getType(uri);
in onActivityResult to identify the mime type.
Now I have added this mime-type and it works as expected.
private void handleImportView() {
Intent intent = new Intent();
String[] mimetypes = {"application/x-sqlite3","application/vnd.sqlite3", "application/octet-stream", "application/x-trash"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select file to import"), REQUEST_CODE_RESTORE);
}