From 96acd09ce9458c5e27ca6e1716789d59a7dd0f1f Mon Sep 17 00:00:00 2001 From: DeSen Xu <2237803016@qq.com> Date: Sun, 5 Feb 2023 13:43:21 +0800 Subject: [PATCH 1/3] update(android): return the 'originFilepath' field when select the image --- .../src/main/java/com/imagepicker/Utils.java | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/imagepicker/Utils.java b/android/src/main/java/com/imagepicker/Utils.java index f3f46f257..6ce87a679 100644 --- a/android/src/main/java/com/imagepicker/Utils.java +++ b/android/src/main/java/com/imagepicker/Utils.java @@ -8,6 +8,7 @@ import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; +import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.hardware.camera2.CameraCharacteristics; @@ -418,6 +419,14 @@ static ReadableMap getImageResponseMap(Uri uri, Options options, Context context return map; } + static ReadableMap getImageResponseMap(Uri tempFileUri, String originFilePath, Options options, Context context) { + WritableMap imageResponseMap = (WritableMap) getImageResponseMap(tempFileUri, options, context); + if (originFilePath != null) { + imageResponseMap.putString("originFilepath", originFilePath); + } + return imageResponseMap; + } + static ReadableMap getVideoResponseMap(Uri uri, Options options, Context context) { String fileName = uri.getLastPathSegment(); WritableMap map = Arguments.createMap(); @@ -449,11 +458,15 @@ static ReadableMap getResponseMap(List fileUris, Options options, Context c // Call getAppSpecificStorageUri in the if block to avoid copying unsupported files if (isImageType(uri, context)) { + String originPath; if (uri.getScheme().contains("content")) { + originPath = getImageFilepathFromContentProtocol(uri, context); uri = getAppSpecificStorageUri(uri, context); + } else { + originPath = uri.toString(); } uri = resizeImage(uri, context, options); - assets.pushMap(getImageResponseMap(uri, options, context)); + assets.pushMap(getImageResponseMap(uri, originPath, options, context)); } else if (isVideoType(uri, context)) { if (uri.getScheme().contains("content")) { uri = getAppSpecificStorageUri(uri, context); @@ -470,6 +483,28 @@ static ReadableMap getResponseMap(List fileUris, Options options, Context c return response; } + /** + * get the image filepath from the 'content' protocol. + * @param uri the uri. + * @param context the context. + * @return the filepath. return null when the file is not exist. + *

+ * the return value may like this: + *

+ * /storage/emulated/0/Pictures/-4fae8f0985703cbe.jpg + */ + private static String getImageFilepathFromContentProtocol(Uri uri, Context context) { + String[] proj = { MediaStore.Images.Media.DATA }; + try (Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null)) { + int index = cursor.getColumnIndex(MediaStore.Images.Media.DATA); + if (index == -1) { + return null; + } + cursor.moveToFirst(); + return cursor.getString(index); + } + } + static ReadableMap getErrorMap(String errCode, String errMsg) { WritableMap map = Arguments.createMap(); map.putString("errorCode", errCode); From ff24ddb37348a303a6a7797b0bacdb1c4b39a01e Mon Sep 17 00:00:00 2001 From: DeSen Xu <2237803016@qq.com> Date: Sun, 5 Feb 2023 14:01:10 +0800 Subject: [PATCH 2/3] update: README.md and types.ts --- README.md | 30 +++++++++++++++++------------- src/types.ts | 4 ++++ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 405a162d4..96b3b06d3 100644 --- a/README.md +++ b/README.md @@ -107,19 +107,23 @@ The `callback` will be called with a response object, refer to [The Response Obj ## Asset Object -| key | iOS | Android | Web | Photo/Video | Requires Permissions | Description | -| --------- | --- | ------- | --- | ----------- | -------------------- | ------------------------- | -| base64 | OK | OK | OK | PHOTO ONLY | NO | The base64 string of the image (photos only) | -| uri | OK | OK | OK | BOTH | NO | The file uri in app specific cache storage. Except when picking **video from Android gallery** where you will get read only content uri, to get file uri in this case copy the file to app specific storage using any react-native library. For web it uses the base64 as uri. | -| width | OK | OK | OK | BOTH | NO | Asset dimensions | -| height | OK | OK | OK | BOTH | NO | Asset dimensions | -| fileSize | OK | OK | NO | BOTH | NO | The file size | -| type | OK | OK | NO | BOTH | NO | The file type | -| fileName | OK | OK | NO | BOTH | NO | The file name | -| duration | OK | OK | NO | VIDEO ONLY | NO | The selected video duration in seconds | -| bitrate | --- | OK | NO | VIDEO ONLY | NO | The average bitrate (in bits/sec) of the selected video, if available. (Android only) | -| timestamp | OK | OK | NO | BOTH | YES | Timestamp of the asset. Only included if 'includeExtra' is true | -| id | OK | OK | NO | BOTH | YES | local identifier of the photo or video. On Android, this is the same as fileName | +| key | iOS | Android | Web | Photo/Video | Requires Permissions | Description | +|----------------|--------|---------|-----|-------------|----------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| base64 | OK | OK | OK | PHOTO ONLY | NO | The base64 string of the image (photos only) | +| uri | OK | OK | OK | BOTH | NO | The file uri in app specific cache storage. Except when picking **video from Android gallery** where you will get read only content uri, to get file uri in this case copy the file to app specific storage using any react-native library. For web it uses the base64 as uri. | +| width | OK | OK | OK | BOTH | NO | Asset dimensions | +| height | OK | OK | OK | BOTH | NO | Asset dimensions | +| fileSize | OK | OK | NO | BOTH | NO | The file size | +| type | OK | OK | NO | BOTH | NO | The file type | +| fileName | OK | OK | NO | BOTH | NO | The file name | +| duration | OK | OK | NO | VIDEO ONLY | NO | The selected video duration in seconds | +| bitrate | --- | OK | NO | VIDEO ONLY | NO | The average bitrate (in bits/sec) of the selected video, if available. (Android only) | +| timestamp | OK | OK | NO | BOTH | YES | Timestamp of the asset. Only included if 'includeExtra' is true | +| id | OK | OK | NO | BOTH | YES | local identifier of the photo or video. On Android, this is the same as fileName | +| originFilepath | **NO** | OK | NO | BOTH | YES | thr origin file path **when select image** | + +**waring:** the `uri` is point at a temporary file! When you pick a same image twice, it will be two different `uri`, +the `fileName` is the temporary file's name. ## Note on file storage diff --git a/src/types.ts b/src/types.ts index dd7d32992..30e4c399d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -40,6 +40,10 @@ export interface Asset { bitrate?: number; timestamp?: string; id?: string; + /** + * the origin filepath when select image.(android only) + */ + originFilepath?: string } export interface ImagePickerResponse { From 2a79ddb4e4383e2c09dd2306156b90a57402183f Mon Sep 17 00:00:00 2001 From: DeSen Xu <72915970+IceOfSummer@users.noreply.github.com> Date: Sun, 5 Feb 2023 15:16:00 +0800 Subject: [PATCH 3/3] fix: incorrect 'Asset Object' description --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 96b3b06d3..acea9222f 100644 --- a/README.md +++ b/README.md @@ -120,10 +120,10 @@ The `callback` will be called with a response object, refer to [The Response Obj | bitrate | --- | OK | NO | VIDEO ONLY | NO | The average bitrate (in bits/sec) of the selected video, if available. (Android only) | | timestamp | OK | OK | NO | BOTH | YES | Timestamp of the asset. Only included if 'includeExtra' is true | | id | OK | OK | NO | BOTH | YES | local identifier of the photo or video. On Android, this is the same as fileName | -| originFilepath | **NO** | OK | NO | BOTH | YES | thr origin file path **when select image** | +| originFilepath | **NO** | OK | NO | PHOTO ONLY | YES | thr origin file path **when select image** | **waring:** the `uri` is point at a temporary file! When you pick a same image twice, it will be two different `uri`, -the `fileName` is the temporary file's name. +the `fileName` also to be the temporary file's name. ## Note on file storage