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.
Related
I am working on an application in which a user selects a pdf file and then it gets uploaded on firebase. The problem is when a user wants to select a file then other files which are not pdf are also shown. I want to restrict it and want to show pdf files only.
I am using this code to select pdf files only but its showing other files as well.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
startActivityForResult(intent.createChooser(intent, "Select PDF file"), FILE_CHOOSER);
How can i do it? Any help?
I am using this code to open PDF files only.
String[] supportedMimeTypes = {"application/pdf"};
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent.setType(supportedMimeTypes.length == 1 ? supportedMimeTypes[0] : "*/*");
if (supportedMimeTypes.length > 0) {
intent.putExtra(Intent.EXTRA_MIME_TYPES, supportedMimeTypes);
}
} else {
String mimeTypes = "";
for (String mimeType : supportedMimeTypes) {
mimeTypes += mimeType + "|";
}
intent.setType(mimeTypes.substring(0,mimeTypes.length() - 1));
}
startActivityForResult(intent, FILE_CHOOSER);
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/*")
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 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);
I want to share multiple images with other application using share action provided by android. The below code does not works as intended.
Sending Activity:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
//intent.putExtra(Intent.EXTRA_SUBJECT, "Here are some files.");
intent.setType("image/*");
ArrayList<Uri> files = new ArrayList<Uri>();
for (String path : imageArray) {
File file = new File(path);
Uri uri = Uri.fromFile(file);
files.add(uri);
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
shareAction.setShareIntent(intent);
Receiving activity code:
if (Intent.ACTION_SEND_MULTIPLE.equals(intent.getAction())
&& intent.hasExtra(Intent.EXTRA_STREAM)) {
ArrayList<Parcelable> list =
intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
for (Parcelable p : list) {
Uri uri = (Uri) p;
System.out.println("hello world");
}
}
Receiving activity output :
file:///absolute-path-to-file
I want output in content://absolute-path-to-file format. Because i am getting this format when receiving file intent from other applications.
Other possible way to solve this is also recommended.