Intent to read both .xls and xlsx files in Android? - android

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

Related

What is the mime type for sqlite db backup in android 11?

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

select only specific file type using intent.getContent method

I am trying to select some files from the device on specific event using following code.
For Audio i used:
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "Select ,Music"),AUDIO_REQUEST);
But Problem is when user tries to select from es file explorer it also allows other file types to be selected. How can I prevent other file types to be selected and allow only audio types. The screendshot is here.
Edit
I also wanted to select document file type such as doc ppt xl etc.
for that I have used
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"};
intent.setAction(Intent.ACTION_GET_CONTENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
} else {
String mimeTypesStr = "";
for (String mimeType : mimeTypes) {
mimeTypesStr += mimeType + "|";
}
intent.setType(mimeTypesStr.substring(0, mimeTypesStr.length() - 1));
}
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "select Document"),DOCUMENT_REQUEST);
But, Same thing with happened to this also.
Can Anyone please help me with this problem?
Try
intent.setType("audio/mpeg");
or
intent.setType("audio/mp3");
or try like this
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/*")

How to choose only doc,docx files through intent?

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"};

Intent ACTION_OPEN_DOCUMENT not filltering for RTF

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.

Correct intent to show only Audio files

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

Categories

Resources