Skip to content

Commit

Permalink
feat(android): add maxImages and pathOnly to openPhotoGallery (#14086)
Browse files Browse the repository at this point in the history
* feat(android): add maxImages to openPhotoGallery

* pathOnly
  • Loading branch information
m1ga authored Aug 21, 2024
1 parent dfb1b5c commit 3eda594
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ public class MediaModule extends KrollModule implements Handler.Callback
private static String mediaType = MEDIA_TYPE_PHOTO;
private static ContentResolver contentResolver;
private boolean useCameraX = false;
private static boolean pathOnly = false;

public MediaModule()
{
Expand Down Expand Up @@ -1116,6 +1117,15 @@ public void openPhotoGallery(KrollDict options)
TiIntentWrapper galleryIntent = new TiIntentWrapper(new Intent());
galleryIntent.getIntent().setAction(Intent.ACTION_GET_CONTENT);

if (options.containsKeyAndNotNull(TiC.PROPERTY_MAX_IMAGES)
&& options.containsKey(TiC.PROPERTY_ALLOW_MULTIPLE)
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
// set max image count
galleryIntent = new TiIntentWrapper(new Intent(MediaStore.ACTION_PICK_IMAGES));
galleryIntent.getIntent()
.putExtra(MediaStore.EXTRA_PICK_IMAGES_MAX, options.getInt(TiC.PROPERTY_MAX_IMAGES));
}

boolean isSelectingPhoto = false;
boolean isSelectingVideo = false;
if (options.containsKey(TiC.PROPERTY_MEDIA_TYPES)) {
Expand Down Expand Up @@ -1163,6 +1173,11 @@ public void openPhotoGallery(KrollDict options)
galleryIntent.getIntent().putExtra(Intent.EXTRA_ALLOW_MULTIPLE, allowMultiple);
}

pathOnly = false;
if (options.containsKeyAndNotNull(TiC.PROPERTY_PATH_ONLY)) {
pathOnly = options.getBoolean(TiC.PROPERTY_PATH_ONLY);
}

final int code = allowMultiple ? PICK_IMAGE_MULTIPLE : PICK_IMAGE_SINGLE;

activitySupport.launchActivityForResult(galleryIntent.getIntent(), code, new TiActivityResultHandler() {
Expand Down Expand Up @@ -1401,16 +1416,18 @@ protected static KrollDict createDictForImage(TiBlob imageData, String mimeType)
d.put("width", width);
d.put("height", height);

// Add the image/video's crop dimensiosn to the dictionary.
// Add the image/video's crop dimension to the dictionary.
KrollDict cropRect = new KrollDict();
cropRect.put("x", 0);
cropRect.put("y", 0);
cropRect.put("width", width);
cropRect.put("height", height);
d.put("cropRect", cropRect);

d.put("path", imageData.getNativePath());
// Add the blob to the dictionary.
d.put("media", imageData);
if (!pathOnly) {
d.put("media", imageData);
}
return d;
}

Expand Down
2 changes: 2 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiC.java
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ public class TiC
public static final String PROPERTY_MAX_AGE = "maxAge";
public static final String PROPERTY_MAX_CLASSNAME = "maxClassname";
public static final String PROPERTY_MAX_ELEVATION = "maxElevation";
public static final String PROPERTY_MAX_IMAGES = "maxImages";
public static final String PROPERTY_MAX_LENGTH = "maxLength";
public static final String PROPERTY_MAX_LINES = "maxLines";
public static final String PROPERTY_MAX_ROW_HEIGHT = "maxRowHeight";
Expand Down Expand Up @@ -640,6 +641,7 @@ public class TiC
public static final String PROPERTY_PASSWORD = "password";
public static final String PROPERTY_PASSWORD_MASK = "passwordMask";
public static final String PROPERTY_PATH = "path";
public static final String PROPERTY_PATH_ONLY = "pathOnly";
public static final String PROPERTY_PERSISTENT = "persistent";
public static final String PROPERTY_PHONE = "phone";
public static final String PROPERTY_PIN_IMAGE = "pinImage";
Expand Down
20 changes: 20 additions & 0 deletions apidoc/Titanium/Media/Media.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2235,6 +2235,20 @@ properties:
osver: {ios: {min: "14.0"}}
since: { android: "6.0.0", iphone: "9.2.0", ipad: "9.2.0" }

- name: maxImages
summary: Specifies the number of images a user can select at maximum.
description: |
Only available on Android API 21 and above and with `allowMultiple:true`
type: Boolean
platforms: [android]
since: { android: "12.5.0" }

- name: pathOnly
summary: Do not include the blob in the result
type: Boolean
platforms: [android]
since: { android: "12.5.0" }

- name: selectionLimit
summary: Specifies number of media item that can be selected.
description: |
Expand Down Expand Up @@ -2284,6 +2298,12 @@ properties:
summary: The media object, as a [Blob](Titanium.Blob).
type: Titanium.Blob

- name: path
summary: The path of the image when returning data from the gallery.
type: String
platforms: [android]
since: "12.5.0"

- name: mediaType
summary: The type of media, either `MEDIA_TYPE_PHOTO`, `MEDIA_TYPE_LIVEPHOTO` or `MEDIA_TYPE_VIDEO` defined in <Titanium.Media>.
type: String
Expand Down

0 comments on commit 3eda594

Please sign in to comment.