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"));
Related
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.
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'm working on a functionality for an Android-Application, which downloads a file from a local server using the Android Download-Manager. Up to now I easily download the file to the downloads directory by using
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,filename);
Now I would like to let the user choose the target including the filename. Therefore I use the ACTION_CREATE_DOCUMENTIntent:
void pickTargetFile() {
Intent intent = new Intent()
.setAction(Intent.ACTION_CREATE_DOCUMENT)
.setType("image/jpeg");
startActivityForResult(Intent.createChooser(intent, "Select a target to save the file"), SAVE_FILE_CODE);
}
But when I now use the resulting path in the download-manager it doesn't write the data in the created file but creates a new one named e.g. filename-1.jpeg when filename.jpeg was created by the File-Create-Dialog.
Either I would like the download-manager to write into the previously created file or use another dialog to choose a target for the download-manager but not creating the file by the dialog.
Does someone know an easy way to archive this?
I have a simple question but can't seem to find the answer anywhere I look online.
I have an activity A that downloads files from the internet and puts them to a local folder obtained by getApplicationContext().getFilesDir().getPath() which points to: "/data/data/com.myapp.android/files"
Now when the user selects a file I want to show its content and for that I do the following:
File file = new File(filePath);
Uri uri = Uri.fromFile(file);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getMimeTypeFromExtension(fileExtension);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(uri, type);
Intent intentChooser = Intent.createChooser(intent, "Open File");
startActivity(intentChooser);
When this executes the user is presented with the Open File dialog and after they select "Gallery" the Gallery activity appears with nothing on it.
Looking at logcat I see the following:
E/PanoMetadata(17732): Could not read file: /data/data/com.myapp.android/files/downloads/Splash.png
E/PanoMetadata(17732): java.io.FileNotFoundException: /data/data/com.myapp.android/files/downloads/Splash.png: open failed: EACCES (Permission denied)
E/PanoMetadata(17732): at libcore.io.IoBridge.open(IoBridge.java:409)
E/PanoMetadata(17732): at java.io.FileInputStream.<init>(FileInputStream.java:78)
Note the "open failed: EACCES (Permission denied)"
But the file is really there and I can display it using my activity A without a problem.
The problem seems to be that the other activity which starts using ACTION_VIEW (in this case Gallery) seems to run under different process/user so it does not have read access to my file.
My 3 questions are:
Is it possible to get the ACTION_VIEW activity to run within my process and user context (of activity A)?
How do I share files between activities? (I do not like to copy the file under /sdcard/... but if that is the only option than it will have to be.)
Is there a way to pass the file content (not file pointer) from my activity to the Gallery activity or other activities?
Regards!
Is it possible to get the ACTION_VIEW activity to run within my process and user context (of activity A)?
No, nor would you want it to, for security reasons.
How do I share files between activities?
This question is the same as your next one, as far as I can tell.
Is there a way to pass the file content (not file pointer) from my activity to the Gallery activity or other activities?
The best solution is to use a ContentProvider. The simplest solution is to use FileProvider, which you can just add to your manifest and configure to serve up your files, without any need to create a subclass. See also: http://developer.android.com/training/secure-file-sharing/index.html
An app can't acces the private data of another app. Being that way, "Gallery" can't reach "data/data/com.myapp.android/files". You need to put the shared files directory in the external storage.
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.