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(android): implement Android Photo Picker #14131

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft

Conversation

m1ga
Copy link
Contributor

@m1ga m1ga commented Oct 3, 2024

Update: As mentioned in a comment below: the current picker will use the new Photo Picker already, so no changes are required. Still keeping this code as it adds maxImages and we'll might want to switch later


Will replace the current openPhotoGallery method and use https://developer.android.com/training/data-storage/shared/photopicker

When I was updating an Android app I was asked in the store to use the Photo Picker instead of using <uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>. But you could still opt-in for the image permission, so not a high priority for now.

I have to call ActivityResultLauncher in the BaseActivity otherwise I wasn't able to catch the results. And in order to get the results from the BaseActivity back to MediaModule I use a local Broadcast. Not sure if that is the best way to do it but I didn't find another way.

const win = Ti.UI.createWindow({layout:"vertical"});
const img = Ti.UI.createImageView({widht: 100,height:100});
const btn1 = Ti.UI.createButton({title: "image - single"});
const btn2 = Ti.UI.createButton({title: "image - multiple"});
const btn3 = Ti.UI.createButton({title: "image - multiple max 3"});
const btn4 = Ti.UI.createButton({title: "video - single"});
const btn5 = Ti.UI.createButton({title: "video - multiple"});
const btn6 = Ti.UI.createButton({title: "video - multiple max 3"});

btn1.addEventListener("click", () => openGal(false, false, 0));
btn2.addEventListener("click", () => openGal(true, false, 0));
btn3.addEventListener("click", () => openGal(true, false, 3));
btn4.addEventListener("click", () => openGal(false, true, 0));
btn5.addEventListener("click", () => openGal(true, true, 0));
btn6.addEventListener("click", () => openGal(true, true, 3));


function openGal(isMultiple, isVideo, maxImages) {
	Ti.Media.openPhotoGallery({
		allowMultiple: isMultiple,
		maxImages: maxImages,
		mediaTypes: isVideo ? Titanium.Media.MEDIA_TYPE_VIDEO : Titanium.Media.MEDIA_TYPE_PHOTO,
		success: function(e) {
			if (!isVideo) {
				if (isMultiple) {
					console.log("Multiple images");
					console.log(e.images.length);
					img.image = e.images[0].media;
				} else {
					console.log("Single image");
					img.image = e.media;
				}
			} else {
				console.log("video")
				if (isMultiple) {
					console.log("Multiple videos");
					e.videos.forEach(function(item) {
						console.log(item.width, item.height)
					})
				} else {
					console.log("Single video");
					console.log(e.width, e.height)
				}
			}
		}
	});
}

win.add([btn1,btn2,btn3,btn4,btn5,btn6,img]);
win.open();

@m1ga
Copy link
Contributor Author

m1ga commented Dec 14, 2024

I think I have all parts now 😄 Image, Video, Image & Video. Multiple and single. Multiple with maxImages

@m1ga m1ga marked this pull request as ready for review December 14, 2024 18:05
@m1ga m1ga marked this pull request as draft January 3, 2025 11:06
@m1ga m1ga marked this pull request as ready for review January 5, 2025 14:57

Log.d(TAG, "openPhotoGallery called", Log.DEBUG_MODE);

Activity activity = TiApplication.getInstance().getCurrentActivity();
TiActivitySupport activitySupport = (TiActivitySupport) activity;

TiIntentWrapper galleryIntent = new TiIntentWrapper(new Intent());
galleryIntent.getIntent().setAction(Intent.ACTION_GET_CONTENT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m1ga Under the hood ACTION_GET_CONTENT already invokes new Photo picker when it's available, otherwise it invokes system picker. I tested it out on 12.5.1.GA on Android 15 device and it invokes new Photo picker.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scroll down to ACTION_GET_CONTENT behaviour change on this page for more details.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah that is good too know! I thought the new Photo Picker is only used when you use PickVisualMediaRequest as they don't mention the ACTION_GET_CONTENT in their page.

Then I guess we don't need to do anything here :-) I'll put this into draft so we don't need to do anything here but still have to code

@m1ga m1ga marked this pull request as draft January 13, 2025 09:08
@prashantsaini1
Copy link
Contributor

@m1ga This major issue is worth keeping an eye even in future as it makes new Photo picker much less useful if it cannot show all albums as of now and unfortunately Google isn't working on this bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants