11

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!

Community
  • 1
  • 1
Gianluca Colombo
  • 717
  • 17
  • 38
  • 1
    You don't have a question, but you are just addressing what you want to do, like a pseudocode, and you expect people here to write the code for you. SO, is not a code writing service. Do your research, try things and code and if you stuck then you ask questions here about specific code problems. See the [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) for more information on how to ask a SO question. – Christos Lytras Mar 15 '17 at 19:53
  • @ChristosLytras here an example where a user asked a question like mine, asking code! I spent a lot of time in research, but I didn't found anything. I just tried, asking is always allowed; answer if you want! . http://stackoverflow.com/questions/10516968/delphi-how-add-text-to-an-image-and-save-the-new-image – Gianluca Colombo Mar 15 '17 at 20:18
  • 2
    Read the second comment there at the question you are linking. Both questions (yours and that link) are not actually asking anything about solving a specific problem, but rather describing a wanted program behavior/feature. Read the [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). And here is a google result by searching for *"android open phone gallery"*, [How to open phones gallery through code](http://stackoverflow.com/a/6016352/1889685). So to me you haven't done any search at all and just came here to seek for someone to do the job for you and offer them bounty. – Christos Lytras Mar 15 '17 at 21:05
  • read the comment in the open bounty please: "I need a working code sample written in Delphi" Unfortunately I don't know how make a conversion from Java to Pascal. Anyway the link you posted is not what I need. I can open the gallery as well but the limiet is I get one image a time – Gianluca Colombo Mar 15 '17 at 21:42
  • Whats minimum version of Android are you targeting? – Nick Cardoso Mar 19 '17 at 19:09
  • The minimum target should be Android 18 at least – Gianluca Colombo Mar 21 '17 at 23:01

1 Answers1

5

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

Community
  • 1
  • 1
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
  • `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, the selected files in delphi! – Gianluca Colombo Mar 20 '17 at 17:43
  • Your answer helped me a bit but not totally. I'm still stuck to find a solution to get back the selected files in delphi. – Gianluca Colombo Mar 23 '17 at 11:15