Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi Firemonkey select multiple pictures from gallery at the same time

I need to create a function in my Android App that allows user to open the phone gallery, select more than one picture a time, then save the selected pictures in my local DB. What I need is the way to use Android Intent to get the selected pictures (files name and path). Hope you can understand my question.

I'm using this code:

if TPlatformServices.Current.SupportsPlatformService(IFMXTakenImageService,
  IInterface(ImageService)) then
begin
  Params.RequiredResolution := TSize.Create(640, 640);
  Params.OnDidFinishTaking := DoDidFinish;
  ImageService.TakeImageFromLibrary(SpeedButton2, Params);
end;

procedure TfGallery.DoDidFinish(Image: TBitmap);
begin
 Image1.Bitmap.Assign(Image);
end;

Unfortunately this code can return one image a time from gallery.

Edit - Based on the answer of Nick Cardoso, the following code works for the first part of the problem:

Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_PICK);
intent.setType(StringToJString('image/*'));
intent.setAction(TjIntent.JavaClass.ACTION_GET_CONTENT);
Intent.putExtra(TJIntent.JavaClass.EXTRA_ALLOW_MULTIPLE,true);
LaunchActivity(Intent);

The code above works to select multiple pictures. Now I'm stuck to find a solution to get back (in a callback function) the selected files in Delphi!

like image 760
Gianluca Colombo Avatar asked Mar 12 '17 21:03

Gianluca Colombo


People also ask

How do I select multiple photos in gallery?

How to select multiple photos on Android. Selecting all the photos saved locally on an Android phone is a lot easier than doing so on an iPhone: On stock Android, open the Files app and go to Images, tap the three dots in the top right corner of your screen, and hit Select all.

How to select multiple pictures on an Android phone?

On an android device you can select multiple images the following way: 1) Firstly, select 'choose your photos' and then select 'my device' . 2) Select on the image folder in which you want to upload multiple images from. 3) Press and hold on the first photo, you should then enter multi-select mode.


1 Answers

I'll start with a Disclaimer - I don't write Delphi. Your question was the first time I've heard of Firemonkey and I expect that the same is true of the majority of Android Developers (hence the low answer rate).

My understanding is that behind the scenes Firemonkey fires off ordinary Android Intents to interact with standard components. This means if we can switch the intent to one which return multiple images, we have a solution.

If you are targeting only Android 18 and higher, it's simply a matter of adding the EXTRA_ALLOW_MULTIPLE extra to the existing photo chooser Intent. With pure Android that is as simple as adding the following and reading back the clip data (like in this answer):

pickerIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE);

If you're targeting older Android versions you could instead include a custom library like this one (or one of these) into your project and target that activity with a new intent.

My research shows that Firemonkey allows custom actions, you'll have to research yourself how to implement one, as you'll better understand the code you read.

However this post (which seems like required reading) showed me it is possible to create your own Intents, which means the code inside your initial action will be similar to this (If you can find the sources for the current TakeImageFromLibrary action you can base your code off of that):

Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_PICK);
//OR Intent := TJIntent.JavaClass.init(StringToJString('com.some.library.client.SOME_ACTION'));
Intent.putExtra(TJIntent.JavaClass.EXTRA_ALLOW_MULTIPLE);
LaunchActivity(Intent);

Additional note: default behaviour for selecting multiple in the gallery is long press

like image 187
Nick Cardoso Avatar answered Oct 05 '22 10:10

Nick Cardoso