Open file from specific folder with Intent.ACTION_OPEN_DOCUMENT - java

I would like to open the file picker at a specific path. The user should be able to pick any file on the phone, but the SAF should show a specific folder first. I tried the following but it always opens the downloads folder. Thanks!
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
Uri uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath());
intent.setDataAndType(uri,"image/*");
startActivityForResult(intent, 1);

you need to use the extra EXTRA_INITIAL_URI

Related

Why is the `onSaveInstanceState` function called when trying to open a file with the SAF?

In my app, I try to load a file with the following code :
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("application/octet-stream");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, CODE_FOR_LOADING);
It works : the file picker is correctly displayed and I can choose the file I want to read and I successfully read it.
My problem is that when or just before the file picker is displayed, I can notice my onSaveInstanceStatefunction is called and it doesn't seem logical to me...
Anybody knows why it happens and if a mean to avoid this exists?

How to use intent settype to search text file of custom file extension

I m using following code to open search intent to search text file in external storage:
private void performFileSearch()
{
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*text/*");
startActivityForResult(intent,READ_REQUEST_CODE);
}
But now I am going to create text file of custom file extension, for example textfile.tre,
this file is not search able by this intent.
So i change type to :
intent.setType("*/*");
Now intent is showing all the files. But I want to show only .tre file. What can be done?
Thanks
But I want to show only .tre file
Sorry, but that is not possible. Android, like the Web, works on MIME types, not file extensions.

Android open an external directory using an intent

There is not a straightforward or a clear way to make an implicit intent to open a folder/directory in Android.
Specifically here I want to open getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).
I tried these ones but they will just open a FileManager app, not the directory I want:
val directory = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
val uri = Uri.parse(directory.path)
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.setDataAndType(uri, "*/*")
startActivity(Intent.createChooser(openIntent, "Open Folder"))
Another example:
val directory = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
val uri = Uri.parse(directory.path)
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(uri, "resource/folder")
startActivity(Intent.createChooser(openIntent, "Open Folder"))
Opening 'THE' Downloads folder
If you want to open the downloads folder, you need to use DownloadManager.ACTION_VIEW_DOWNLOADS, like this:
Intent downloadIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
downloadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(downloadIntent);
No need to use mime type resource/folder as Android doesn't have an official folder mime type, so you could produce some errors in some devices. Your device seems not to support that mime type. You need to use the code above, as it just passes to the intent the official folder you want to go to.
Edit: For other custom directories, I don't think that you can just pass a path to the intent like the one. I don't think that there is a reliable way to open a folder in Android.
Using FileProvider(Test)
Edit: Try instead of just parsing the Uri, if you are using FileProvider, which you should, use getUriForFile(). Like this:
val dir = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
val intent = new Intent(Intent.ACTION_VIEW)
val mydir = getUriForFile(context, "paste_your_authority", dir)
intent.setDataAndType(mydir, "resource/folder")
startActivity(intent);
or instead of using resource/folder, use:
DocumentsContract.Document.MIME_TYPE_DIR
Moral of the story:
There is no standard way of opening files. Every device is different and the code above is not guaranteed to work in every single device.

When trying to open a file with Intent getting 'Media not found' error

I download a file and save it as a temporary file using
File outputDir = context.getCacheDir();
File f = File.createTempFile(FILE_TYPE_PREFIX, "." + extension,outputDir);
f.exists() says that the file exists and as well I set f.setReadable(true, false); to be able to read it.
The I start a new Intent
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(f), mimeType);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
the f returns
/data/user/0/com.app.name/cache/PP_278545395.png
and after Uri.fromFile()
file:///data/user/0/com.app.name/cache/PP_278545395.png
As I try to open the file, it opens the Gallery but tells me "Media not found".
Any ideas why this issue exists?
getCacheDir() is part of internal storage for your app. Other apps do not have access to it. Use FileProvider to publish that content to other apps.

When using Android file provider, files don't have correct permissions despite FLAG_GRANT_WRITE_URI_PERMISSION being flagged in intent

I'm trying to load documents from files in my app using Microsoft Word and PDF viewers and I'm using a FileProvider to handle Android 7.0+ not allowing file URIs to be passed freely. I get the URI like so and and set the Intent flags to allow reading and writing before opening it, like so:
// From the byte array create a file containing that data, and get extension and MIME type.
File fileToOpen = byteArrayToFile(documentData, shortFileName);
String fileExtension = UtilityMethods.getFileExtension(fileToOpen.getName());
String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
// Get the Uri for the file from the file provider, open using intent.
Uri documentUri = FileProvider.getUriForFile(getContext(), "com.mycompany.provider", fileToOpen);
Intent intent = new Intent();
intent.setDataAndType(documentUri, mime);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setAction(Intent.ACTION_VIEW);
startActivity(intent);
However when the file loads in MS Word the file is read only, and cannot be edited, which is not the desired behaviour. Where am I going wrong?

Categories