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);
}
I'm using Intent to open file manager, I need to know how to show only .doc, .docx files to choose by users. How to put setType to intent?`
The Following function is used to choose file from file manager.
private void showFileChooser() {
Intent intent = new Intent();
//sets the select file to all types of files
intent.setType("application/*");
//allows to select data and return it
intent.setAction(Intent.ACTION_GET_CONTENT);
//starts new activity to select file and return data
startActivityForResult(Intent.createChooser(intent, "Choose File to Upload.."), PICK_FILE_REQUEST);
}`
You can add multiple mime types like this:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
String[] mimetypes = {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/msword"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(intent, REQUEST_CODE_OPEN);
Following mime types corespond to .docx and .doc files
String[] mimetypes = {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/msword"};
I am setting up a file picker intent but its not filtering for RFT files.
private void openFilePicker(){
Intent fileIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
fileIntent.addCategory(Intent.CATEGORY_OPENABLE);
fileIntent.setType("*/*");
String[] mimetypes = {"text/plain", "text/html", "text/richtext", "application/rtf", "application/x-rtf"};
fileIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
String title = getResources().getString(R.string.chooser_title);
Intent chooser = Intent.createChooser(fileIntent, title);
if(fileIntent.resolveActivity(getPackageManager()) != null){
startActivityForResult(chooser, GET_FILE_CODE);
}else{
//display error message here
}
}
This code filters for txt and html files but not for rtf.
I got the mime types from here: https://www.sitepoint.com/web-foundations/mime-types-complete-list/
The list of mime types is missing text/rtf including this allows rtf files to be filtered for.
I have a great problem to resolve on my app.
I'm try pick a custom file or content using the code below
public void openFolderToFile(Fragment fragment, String filter) {
int PICKFILE_RESULT_CODE = 1; //101;
int READ_REQUEST_CODE = 42;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// intent.setAction(Intent.ACTION_GET_CONTENT);
if (intent.hasCategory(Intent.CATEGORY_OPENABLE)) {
intent.removeCategory(Intent.CATEGORY_OPENABLE);
}
if (!intent.hasCategory(Intent.CATEGORY_DEFAULT)) {
intent.addCategory(Intent.CATEGORY_DEFAULT);
}
intent.setFlags(intent.getFlags()
| Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
| Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_PREFIX_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
// intent.addCategory(Intent.ACTION_OPEN_DOCUMENT);
// intent.addCategory(Intent.CATEGORY_OPENABLE);
// intent.addCategory(Intent.CATEGORY_DEFAULT);
// intent.addCategory(Intent.CATEGORY_BROWSABLE);
// intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
// intent.createChooser(target, title)
filter = filter == null ? "file/*" : filter;
intent.setType(filter);
// Uri uri =
// Uri.parse(Environment.getDownloadCacheDirectory().getPath());
// intent.setDataAndType(uri, "text/nhpz,text/nhpz2");
intent.putExtra("CONTENT_TYPE", filter);
fragment.startActivityForResult(intent, PICKFILE_RESULT_CODE);
/*
* fragment.startActivityForResult( intent.createChooser(intent,
* "Select your file"), PICKFILE_RESULT_CODE);
*/
}
And this method can be used of this way...
String filter = "text/nhpz,text/nhpz2";
openFolderToFile(myFragment, filter);
or
String filter = "*/nhpz,*/nhpz2";
openFolderToFile(myFragment, filter);
The String filter could be any others custons extensions.
This code work fine, but in some devices when I try pick my custom file directly of download folder without using external apps my file cannot be selected.
I'm try any others combinations of similar codes, when you can see commented and the result is the same.
I can selected my file or content using externals apps during the process, but if I try selected directly of my sdcard or download folder I can't, and I need make this on my application, because I can not force the user to install an external application to use with my app.
Does someone have any idea to help me?
I would like to start an intentchooser for apps which can return any kind of file
Currently I use (which I copied from the Android email source code for file attachment)
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
Intent i = Intent.createChooser(intent, "File");
startActivityForResult(i, CHOOSE_FILE_REQUESTCODE);
But it only shows "Gallery" and "Music player" on my Galaxy S2. There is a file explorer on this device and I would like it to appear in the list. I would also like the camera app to show in the list, so that the user can shoot a picture and send it through my app.
If I install Astro file manager it will respond to that intent, too. My customers are Galaxy SII owners only and I don't want to force them to install Astro file manager given that they already have a basic but sufficient file manager.
Any idea of how I could achieve this ? I am pretty sure that I already saw the default file manager appear in such a menu to pick a file, but I can't remember in which app.
Not for camera but for other files..
In my device I have ES File Explorer installed and This simply thing works in my case..
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
Samsung file explorer needs not only custom action (com.sec.android.app.myfiles.PICK_DATA),
but also category part (Intent.CATEGORY_DEFAULT) and mime-type should be passed as extra.
Intent intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
intent.addCategory(Intent.CATEGORY_DEFAULT);
You can also use this action for opening multiple files: com.sec.android.app.myfiles.PICK_DATA_MULTIPLE
Anyway here is my solution which works on Samsung and other devices:
public void openFile(String mimeType) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(mimeType);
intent.addCategory(Intent.CATEGORY_OPENABLE);
// special intent for Samsung file manager
Intent sIntent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
// if you want any file type, you can skip next line
sIntent.putExtra("CONTENT_TYPE", mimeType);
sIntent.addCategory(Intent.CATEGORY_DEFAULT);
Intent chooserIntent;
if (getPackageManager().resolveActivity(sIntent, 0) != null){
// it is device with Samsung file manager
chooserIntent = Intent.createChooser(sIntent, "Open file");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { intent});
} else {
chooserIntent = Intent.createChooser(intent, "Open file");
}
try {
startActivityForResult(chooserIntent, CHOOSE_FILE_REQUESTCODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(), "No suitable File Manager was found.", Toast.LENGTH_SHORT).show();
}
}
This solution works well for me, and maybe will be useful for someone else.
this work for me on galaxy note
its show
contacts,
file managers installed on device,
gallery,
music player
private void openFile(Int CODE) {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("*/*");
startActivityForResult(intent, CODE);
}
here get path in onActivityResult of activity.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String Fpath = data.getDataString();
// do somthing...
super.onActivityResult(requestCode, resultCode, data);
}
This gives me the best result:
Intent intent;
if (android.os.Build.MANUFACTURER.equalsIgnoreCase("samsung")) {
intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
intent.addCategory(Intent.CATEGORY_DEFAULT);
} else {
String[] mimeTypes =
{"application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .doc & .docx
"application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation", // .ppt & .pptx
"application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // .xls & .xlsx
"text/plain",
"application/pdf",
"application/zip", "application/vnd.android.package-archive"};
intent = new Intent(Intent.ACTION_GET_CONTENT); // or ACTION_OPEN_DOCUMENT
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
}
Turns out the Samsung file explorer uses a custom action. This is why I could see the Samsung file explorer when looking for a file from the samsung apps, but not from mine.
The action is "com.sec.android.app.myfiles.PICK_DATA"
I created a custom Activity Picker which displays activities filtering both intents.
If you want to know this, it exists an open source library called aFileDialog that it is an small and easy to use which provides a file picker.
The difference with another file chooser's libraries for Android is that aFileDialog gives you the option to open the file chooser as a Dialog and as an Activity.
It also lets you to select folders, create files, filter files using regular expressions and show confirmation dialogs.
The other answers are not incorrect. However, now there are more options for opening files. For example, if you want the app to have long term, permanent acess to a file, you can use ACTION_OPEN_DOCUMENT instead. Refer to the official documentation: Open files using storage access framework. Also refer to this answer.