I want to choose only pdf, xlsx and txt file from storage but intent.setType can do only one file(eg.txt file only (or) pdf file only). Is it possible to get all three files by coding intent.setType() and Is there a way to do?
Here is some of my code.
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select txt file"),
0);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
}
}
It looks like you just want to see if those intents can be resolved. This might be a better approach:
References:
http://developer.android.com/training/basics/intents/sending.html
How do I determine if Android can handle PDF
You can use Intent.ACTION_OPEN_DOCUMENT,
Each document is represented as a content:// URI backed by a
DocumentsProvider
, which can be opened as a stream withopenFileDescriptor(Uri, String)
, or queried forDocumentsContract.Document
metadata.All selected documents are returned to the calling application with persistable read and write permission grants. If you want to maintain access to the documents across device reboots, you need to explicitly take the persistable permissions using
takePersistableUriPermission(Uri, int)
.Callers must indicate the acceptable document MIME types through
setType(String)
. For example, to select photos, useimage/*
. If multiple disjoint MIME types are acceptable, define them inEXTRA_MIME_TYPES
andsetType(String)
to*/*
.For the more details, please refer this link
http://developer.android.com/reference/android/content/Intent.html#ACTION_OPEN_DOCUMENT
Note that this is only available on API Level 19+.
You should use the system function to find the specific mimeType of the file that you are trying to access(Like the "application/pdf"). If you are dealing with any unknown type, it will probably be hard to find the type. Hope this code will save you !
Source HERE
Maybe this is what you want.
@Fatehali Asamadi's way is OK, but need to add something for appropriate use. For Microsoft documents both (.doc or .docx), (.ppt or .pptx), (.xls or .xlsx) extensions are used. To support or browse these extensions you need to add more mimeTypes.
Use below method to browse documents where REQUEST_CODE_DOC is requestCode for onActivityResult(final int requestCode, final int resultCode,final Intent data) @Override method.
You can get clear concept and add your required mimeTypes from Here
You should to check this link http://www.androidsnippets.com/open-any-type-of-file-with-default-intent.html. Also read through Mime Type.
This is how I have implemented for any file or selected mime type file