I am trying to open a directory for user to choose an excel document from it.
However, my file picker opens in the "recent" location.
Is there a way to start it from a specific directory using new versions of Android?
This is my code:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.DIRECTORY_DOCUMENTS);
intent.setDataAndType(uri, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
startActivity(Intent.createChooser(intent, "Open folder"));
You need to set EXTRA_INITIAL_URI:
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
Unfortunately, the Intent.ACTION_GET_CONTENT does not support opening in a specific location. The only way to choose a specific location is by using a third-party file picker library, such as FilePicker, aFileChooser, etc.
You can use these libraries to create a custom file picker that opens in a specific location and allows the user to choose a file. Then, you can call the custom file picker in your code instead of using Intent.ACTION_GET_CONTENT.
Related
I am facing an issue while opening the File Manager from Android apps.
I want to see only particular file only which I have specific but I am able to see other file also but it is not highlight it is been grey out but how to hide that also so only particular specific file I can see no other file.
Below code which I have used:
Intent intent = new Intent(Intent.Action_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES,"application/pdf");
startActivityForResult(intent, FILE_REQUESTED);
What can I do to fix this?
I want to see only particular file only
Sorry, that is not an option with ACTION_OPEN_DOCUMENT.
I wish to select a video from User's gallery app, but I'd like to have one of the directories hidden from the video picker.
Directory "abc/" is visible in user's default gallery
In my app, user will pick a video from gallery
When he's picking a video using his gallery, directory "abc/" won't be visible.
What I currently do shows all directories available in the user gallery:
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
i.setType("video/*");
startActivityForResult(i, MainController.SELECT_VIDEO_REQUEST);
Is there any option that I could add to the intent to prevent my "abc" directory from being visible at picking time?
Is there any option that I could add to the intent to prevent my "abc" directory from being visible at picking time?
Not using ACTION_PICK. You are delegating this work to one of many possible apps. There are no documented extras for the Intent for "please hide this directory" or "please hide all videos whose names contain the letter 'q'" or anything like that. And, even if there were such an extra, apps implementing ACTION_PICK would be free to ignore it.
There are existing libraries for choosing files; one might meet your needs.
Just before you start the picker intent create an empty file with the name .nomedia in the abc directory.
In onActivityResult() delete the file again.
I have designed two buttons.One button for selecting the file and another button for to open the selected file.I have selected the file correctly and the file path also retrieved.But i cant open the particular file directly by the file path.Any one I have tried something like this
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ selectedFilePath);
intent.setDataAndType(uri, "text/csv");
startActivity(Intent.createChooser(intent, "Open folder"));
First, ACTION_GET_CONTENT does not accept a Uri as input.
Second, ACTION_GET_CONTENT has nothing to do with opening a file. Presumably, you should be using ACTION_VIEW.
Third, the value that you are passing to Uri.parse() is not a String form of a Uri.
Also, I would expect few Android devices to have an ACTION_VIEW activity for text/csv content.
I am making an app that downloads a file. After the file finishes downloading, the user can press a button that will open the file location in their default file manager.
I don't want to use intents as it causes the file manager to act like a file chooser! I do NOT want a file picker. My app should get lost after the user clicks on the button after showing him the downloaded location. I will figure out destroying my app later but first I want to fire up the default file manager and point it to the downloaded location. Let me know how to do this.
I used this code but it acts like a file picker.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); //tried ACTION_VIEW but it says no apps that can perform this action!
Uri uri = Uri.fromFile(mDownloadDir); //mDownloadDir is file object
intent.setDataAndType(uri, "resource/folder");
mContext.startActivity(Intent.createChooser(intent, "Open folder"));
I want to programatically launch a default file explorer to show me the contents of a folder.
I'm using this code, but it crashes:
Uri startDir = Uri.fromFile(new File("/sdcard/DCIM/Camera"));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(startDir);
startActivity(intent);
LogCat shows "No Activity found to handle the Intent"...
What's my best option to do this? I want the user to see the contents of the folder and be able to click the files (e.g. click a video and launch it with default player, click a PDF and open it etc).
Unfortunately there seem to be no standard way to do this, I was searching for the exact same thing before and couldn't find out any solution. There are 2 alternative methods that might work for you:
1) Use a general intent and let the user pick his/her file manager
This is safe and easy but a little far from what we really want
Uri startDir = Uri.fromFile(new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/DCIM/Camera"));
Intent intent = new Intent();
intent.setData(startDir);
intent.setType("*/*");
intent.setAction(Intent.ACTION_VIEW);
startActivity(intent);
Don't use intent.setType("file/*");, it's not a standard MIME type.
2) Use Specific Intents that the famous file managers provide The well known file managers have their own custom intent filters that accept a directory path which allows simple browsing. Some of them are here: OI file manager, ES Explorer
Maybe you could check if the user have these specific file managers installed and then use this solution, else fallback to general intent.
For now these are the only two options you have. I will update this post if I find out any better solution.
Although I could not get the OI file manager to go to a specific directory, the following opens up the OI File Manager directly and goes to the last directory it was in.
Intent importFileIntent = new Intent();
importFileIntent.setType( "file/*" );
// Does nothing.
// Uri startingDir = Uri.fromFile( new File(
// Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera"));
// importFileIntent.setData( startingDirectory );
importFileIntent.setAction( Intent.ACTION_GET_CONTENT );
// Ensure that there's an activity to handle the intent.
if (importFileIntent.resolveActivity(getPackageManager()) == null) return;
startActivityForResult(importFileIntent, REQUEST_FILE_IMPORT);
If anybody has further success, I would love to hear it.