Skip to content
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 persistent ContentUrl functionality to pickFile on Android #80

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gold-readers-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capawesome/capacitor-file-picker': minor
---

feat(android): add persistent ContentUrl functionality to pickFile
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions packages/file-picker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,12 @@ Remove all listeners for this plugin.

#### PickFilesOptions

| Prop | Type | Description | Default |
| -------------- | --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------ |
| **`types`** | <code>string[]</code> | List of accepted file types. Look at [IANA Media Types](https://www.iana.org/assignments/media-types/media-types.xhtml) for a complete list of standard media types. This option cannot be used with `multiple: true` on Android. | |
| **`multiple`** | <code>boolean</code> | Whether multiple files may be selected. | <code>false</code> |
| **`readData`** | <code>boolean</code> | Whether to read the file data. | <code>false</code> |
| Prop | Type | Description | Default | Since |
| ----------------------- | --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------ | ----- |
| **`persistContentUri`** | <code>boolean</code> | Request persistent file access for reusing received path after app restart or system reboot. Only available on Android, iOS paths are persistent by default. | <code>false</code> | 5.1.2 |
| **`types`** | <code>string[]</code> | List of accepted file types. Look at [IANA Media Types](https://www.iana.org/assignments/media-types/media-types.xhtml) for a complete list of standard media types. This option cannot be used with `multiple: true` on Android. | | |
| **`multiple`** | <code>boolean</code> | Whether multiple files may be selected. | <code>false</code> | |
| **`readData`** | <code>boolean</code> | Whether to read the file data. | <code>false</code> | |


#### PickMediaOptions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.capawesome.capacitorjs.plugins.filepicker;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
Expand All @@ -16,6 +17,7 @@
import com.getcapacitor.annotation.CapacitorPlugin;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.json.JSONException;

@CapacitorPlugin(name = "FilePicker")
Expand All @@ -39,11 +41,13 @@ public void convertHeicToJpeg(PluginCall call) {
@PluginMethod
public void pickFiles(PluginCall call) {
try {
boolean persistContentUri = call.getBoolean("persistContentUri", false);
String intentAction = persistContentUri ? Intent.ACTION_OPEN_DOCUMENT : Intent.ACTION_GET_CONTENT;
JSArray types = call.getArray("types", null);
boolean multiple = call.getBoolean("multiple", false);
String[] parsedTypes = parseTypesOption(types);

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Intent intent = new Intent(intentAction);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, multiple);
Expand Down Expand Up @@ -139,11 +143,12 @@ private void pickFilesResult(PluginCall call, ActivityResult result) {
if (call == null) {
return;
}
boolean persistContentUri = call.getBoolean("persistContentUri", false);
boolean readData = call.getBoolean("readData", false);
int resultCode = result.getResultCode();
switch (resultCode) {
case Activity.RESULT_OK:
JSObject callResult = createPickFilesResult(result.getData(), readData);
JSObject callResult = createPickFilesResult(result.getData(), persistContentUri, readData);
call.resolve(callResult);
break;
case Activity.RESULT_CANCELED:
Expand All @@ -159,7 +164,8 @@ private void pickFilesResult(PluginCall call, ActivityResult result) {
}
}

private JSObject createPickFilesResult(@Nullable Intent data, boolean readData) {
private JSObject createPickFilesResult(@Nullable Intent data, boolean persistContentUri, boolean readData) {
ContentResolver contentResolver = getContext().getContentResolver();
JSObject callResult = new JSObject();
List<JSObject> filesResultList = new ArrayList<>();
if (data == null) {
Expand All @@ -181,6 +187,9 @@ private JSObject createPickFilesResult(@Nullable Intent data, boolean readData)
if (uri == null) {
continue;
}
if (persistContentUri) {
contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
JSObject fileResult = new JSObject();
if (readData) {
fileResult.put("data", implementation.getDataFromUri(uri));
Expand Down
9 changes: 9 additions & 0 deletions packages/file-picker/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ export interface ConvertHeicToJpegResult {
}

export interface PickFilesOptions {
/**
* Request persistent file access for reusing received path after app restart or system reboot.
*
* Only available on Android, iOS paths are persistent by default.
*
* @since 5.1.2
* @default false
*/
persistContentUri?: boolean;
/**
* List of accepted file types.
* Look at [IANA Media Types](https://www.iana.org/assignments/media-types/media-types.xhtml) for a complete list of standard media types.
Expand Down