I am new in android development and I just want to know how to do a customized file explorer that will only display pdf, text and image files.
I tried this example but I dont know how to filter the files that can be displayed :(
Android File Explore
Is there a way to display and select only the files mentioned above?
Take a look at #Smitted answer.
Create the intent:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
You can set the MIME types for the intent:
intent.SetType("application/pdf");
and add additional MIME types:
String[] mimetypes = {"image/*", "text/*"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
and start the activity:
StartActivityForResult(intent, FIND_FILE_REQUEST_CODE);
You can filter the files in the accept() of new FilenameFilter().
Related
I want to set a particular extension for intent in the android studio.
I have tried intent.setType("*\\.pdf")"
you have tried path to file (any path with pdf extension), thats not a mimeType which you should set in here. try to set proper value intent.setType("application/pdf");
Intent intentPdf = new Intent(Intent.ACTION_GET_CONTENT);
intentPdf.setType("application/pdf");
intentPdf.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intentPdf, "Pick PDF"), CUSTOM_CODE);
Is there a way to remove items like "Downloads", "Pictures" or "Videos" sections from the android file picker?
I know that Google Drive for example can be removed using:
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
But how can you remove the others? I'm trying it on a Samsung device, reading through some posts I found out that there might be vendor-related configurations that you have to specify for the intent. Mine looks like this:
Intent intent = new Intent(Intent.OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.setType("*/*");
Have you tried to specify the MIME type?
Try changing this
intent.setType("*/*");
To this
intent.setType("image/*");
In this case you won't see videos and audio.
Change the MIME type according to what you need.
I'm trying to use ACTION_GET_CONTENT with text/csv mime type but the result is that all files are displayed but grayed out in the file picker?
Here is my code:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/csv");
...
currentActivity.startActivityForResult(intent, READ_REQUEST_CODE, Bundle.EMPTY);
Tested on Android 7.1/8/9 with the same outcome. The following directory contains .json and .csv files.
Is this mime type even supported?
I don't think a csv is explicitly supported, instead you can just treat it as a normal plaintext file, and parse the content yourself like this:
intent.setType("text/*");
The downside is that the non-csv files will also be shown to the user, therefore, you might need to do some extra checking for the contents of the file.
If you really do need it to be a .csv being shown to the user, you can create your own file picking activity that filter everything else that does not have the .csv extension.
I have the same problem. Android seems to only recognize "text/comma-separated-values". Here's how I fixed it,
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
String[] mimetypes = {"text/csv", "text/comma-separated-values"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
How to open default file manager app that let you choose a file but only with extension .epub or .pdf programatically via Intent?
The code shown is the declaration of the intent that you have to start for opening the default file manager app that let you choose a file but only with extension .epub or .pdf, I hope it will be useful.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/epub+zip");
String[] mimetypes = {"application/epub+zip", "application/pdf"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(intent,PICKFILE_RESULT_CODE);
It is a mix of solutions I found that work so maybe it is redundant. Let me know what you think!
I want to search user's phone for files so i am using intent.setType. I am little confused. I don't want to search for video and images,but i want to search for all other files like pdf,text,docx etc.What should i write in intent,setType("");
try this you can specify your intent.setType like this
intent.setType("application/x-excel|application/pdf|text/plain");
Try below code;
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Choose File to Upload.."),PICK_FILE_REQUEST);