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"};
Related
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);
}
My intent picks .xls while
fileintent.setType("application/vnd.ms-excel");
Picks .xlsx while
fileintent.setType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
How to make both .xls and .xlsx selectable and readable? Appreciate help!
For .xls and .xlsx extensions,
Mimetypes are clubbed.
Intent intent;
String[] mimetypes =
{ "application/vnd.ms-excel", // .xls
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" // .xlsx
};
intent = new Intent(Intent.ACTION_GET_CONTENT); // or use ACTION_OPEN_DOCUMENT
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
intent.addCategory(Intent.CATEGORY_OPENABLE);
For other extensions:
Check the following link, find the corresponding mimetype and include it in the above code.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
I am trying to select pdf using intent and get that selected pdf file path I am not sure its right or wrong.
if not then how can I get pdf path in onActivityResult.
.
Intent intent = new Intent();
intent.setType("pdf/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select PDF"), PDF_PICKER_RESULTS);
using application/pdf line i can do this things.
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select PDF"), PDF_PICKER_RESULTS);
When opening a file picker with the following method:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
Intent chooserIntent = Intent.createChooser(intent, "Open file");
startActivityForResult(chooserIntent, REQUEST_CODE_FILE_PICKER);
unless a default has been set, this will present the user with a choice of file pickers to use. How do you make your own, internal, file chooser available as one of the choices of file pickers presented to the user (such as the Material File Picker)?
Thanks to #CommonsWare's comment, here is the extra bit of code needed to add the internal file picker to the list of choices presented to the user:
// this bit is as before...
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
Intent chooserIntent = Intent.createChooser(intent, "Open file");
// new bit... create Intent for the internal file picker...
Intent materialFilePickerIntent = new Intent(this, FilePickerActivity.class);
materialFilePickerIntent.putExtra(FilePickerActivity.ARG_FILE_FILTER, Pattern.compile(".*\\.txt$"));
// and add the picker to the list...
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { materialFilePickerIntent });
// finally, startActivityForResult() as before...
startActivityForResult(chooserIntent, REQUEST_CODE_FILE_PICKER);
I am trying to pick Audio from Gallery. I am using the following intent.
private void selectAudioFromGallery() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("audio/3gp, audio/AMR, audio/mp3");
startActivityForResult(Intent.createChooser(intent, getString(R.string.select_audio_file_title)), REQ_CODE_PICK_SOUNDFILE);
}
I want to select files of type 3gp, AMR, mp3 and only these files should be listed in the file chooser but the problem I am facing it, it also shows the other files too like text files, image etc.
Intent intent = null;
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
intent = new Intent(Intent.ACTION_GET_CONTENT);
}else {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
}
// The MIME data type filter
intent.setType("audio/*")
Works..
Use this
intent.setType("audio/3gp|audio/AMR|audio/mp3");
Adding multiple mime type need pipe sign, not comma.
Another way
intent.setType("audio/*");
String[] mimetypes = {"audio/3gp", "audio/AMR", "audio/mp3"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);