-
-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(file-picker): add pickDirectory(...)
method
#16
feat(file-picker): add pickDirectory(...)
method
#16
Comments
The solution IMO is to upgrade the API used to access files. https://developer.android.com/guide/topics/providers/document-provider#client
|
For this, you can use a specific intent: Intent.ACTION_OPEN_DOCUMENT_TREE . I made this working on the organice app: 200ok-ch/organice#933 I am using the document library but I believe it's easier to implement the features using android code - as not to depend on the library ?!
There is also example on how to persist permissions across Android reboots. @PluginMethod
public void pickDirectory(PluginCall call) {
// Sample code from https://github.com/android/storage-samples/blob/main/StorageClient/Application/src/main/java/com/example/android/storageclient/StorageClientFragment.java
// BEGIN_INCLUDE (use_open_document_intent)
// ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file browser.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
// Optionally, specify a URI for the directory that should be opened in
// the system file picker when it loads.
// intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uriToLoad);
// https://capacitorjs.com/docs/plugins/android#intents-with-results
startActivityForResult(call, intent, "pickDirectoryResult");
}
/**
* Companion callback to {@link OrganiceSync::pickDirectory} .
* Handles the result of "pick a directory" action.
* <p>
* Will save permissions to the directory.
*
* @param call
* @param result
*/
@SuppressLint("WrongConstant")
@ActivityCallback
public void pickDirectoryResult(PluginCall call, ActivityResult result) {
if (call == null) {
return;
}
Intent intent = result.getData();
if (intent != null) {
Uri uri = intent.getData();
JSObject ret = new JSObject();
// Persist permissions
// https://developer.android.com/training/data-storage/shared/documents-files#grant-access-directory
final int takeFlags = intent.getFlags()
& (Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// Check for the freshest data.
getActivity().getContentResolver().takePersistableUriPermission(uri, takeFlags);
var d = DocumentFile.fromTreeUri(getContext(),uri);
ret.put("uri", uri);
ret.put("path", d.getUri().getEncodedPath());
call.resolve(ret);
} else {
JSObject ret = new JSObject();
ret.put("intent", result.getData());
ret.put("resultCode", result.getResultCode());
ret.put("uri", null);
call.reject("Failed with intent code " + result.getResultCode(), ret);
}
} |
Thank you for your input! That will help me a lot. |
Well, it would be great to have this working properly for Android and IOs. We need basic operations for organice:
|
Guys, is there any chance that 'open directory' feature will be added for file picker ? Would be extremely nice to have |
@Nikita-schetko Yes, but there is no ETA. PRs are welcome. |
pickDirectory(...)
method
I would like to be able to pick a directory instead of a file.
I'm working on adding Android local storage support for organice https://github.com/200ok-ch/organice/ .
We need the ability to select the directory that contains the org files to manage.
Thanks,
Eugen
The text was updated successfully, but these errors were encountered: