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);
Related
Is there any library to upload different type of files to NodeJS server back-end.
I need to send following three different types of files
*.png
*.xls
*.pdf
I have solution for same files sending to backend. But how to do with different files?
This may help you
String[] mimeTypes =
{"application/vnd.ms-excel","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // .xls & .xlsx
"application/pdf",
"image/*"};
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.addCategory(Intent.CATEGORY_OPENABLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent.setType(mimeTypes.length == 1 ? mimeTypes[0] : "*/*");
if (mimeTypes.length > 0) {
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
}
} else {
String mimeTypesStr = "";
for (String mimeType : mimeTypes) {
mimeTypesStr += mimeType + "|";
}
intent.setType(mimeTypesStr.substring(0,mimeTypesStr.length() - 1));
}
startActivityForResult(Intent.createChooser(intent,"ChooseFile"), REQUEST_CODE_DOC);
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);
How to use intent to prompt user to choose "finish action with" to choosing app to select file (assuming there is a few app to browse file in the device)
I want to filter the file using an extension.. (eg. : *.sav, *.props)
Thank you in advance
You can use something like this:
....
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, YOUR_RESULT_CODE);
....
But I really doubt that you can set a filter third-party file browsers.
Or you can try to use this file dialog: http://code.google.com/p/android-file-dialog/
this will open the build-in file explorer if available, otherwise will ask u to select a file explorer that u have installed.
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//intent.setType("*/*"); //all files
intent.setType("text/xml"); //XML file only
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
}
}
It may help you to choose a doc file and u may change action as per your need
Intent intent;
if (VERSION.SDK_INT >= 19) {
intent = new Intent("android.intent.action.OPEN_DOCUMENT");
intent.setType("*/*");
} else {
PackageManager packageManager =getActivity().getPackageManager();
intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("file*//*");
if (packageManager.queryIntentActivities(intent,MEDIA_TYPE_IMAGE).size() == 0) {
UserToast.show(getActivity(), getResources().getString(R.string.no_file_manager_present));
}
}
if (getActivity().getPackageManager().resolveActivity(intent, NativeProtocol.MESSAGE_GET_ACCESS_TOKEN_REQUEST) != null) {
startActivityForResult(intent, UPLOAD_FILE);
}
// check here to KitKat or new version and this will solve the Samsung file explore issue too.
boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
if (isKitKat) {
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,FILE_SELECT_CODE);
} else {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent,FILE_SELECT_CODE);
}