Jump to content
Sign in to follow this  
Dave Nottage

[Android] Retrieving photos from the camera roll that have been taken in a Delphi app

Recommended Posts

I'm having an issue with being able to retrieve photos from the camera roll when using TTakePhotoFromCameraAction. It's probably not a bug; it's more likely a case of "yet to be implemented" ūüôā


I've attached a project that demonstrates the issue. On startup the test app requests permission to read external storage, then if granted proceeds to retrieve the photos from the camera roll. This works fine.


When the TakePhoto button is clicked, the app requests permissions for the camera and to write to external storage, then proceeds to execute the action. When the user has finished taking the photo, the TakePhotoFromCameraActionDidFinishTaking event fires, and the app reloads the photos from the camera roll. One would expect the newly taken photo to be in the roll, however it is not.


Restarting the app still does not show the new photo. Running the Photos app on the device shows the new photo, however with a slight delay before it actually appears - I'm not sure if this is a clue. Restart the test app again, et voila! The new photo is there.


The question is: why does the photo not appear when restarting the app (not that I'd expect anyone to do this), and not until the Photos app has been run?




Share this post

Link to post

After a lot of Googling and experimenting, I managed to solve this myself.


From what I could gather, and this may apply only to later versions of Android (I have Android 10 on my Pixel 3a), the method that FMX uses to "scan" the image file into the gallery, which is via an intent (source\rtl\androiddex\java\fmx\src\com\embarcadero\firemonkey\medialibrary\MediaImage.java):

     * Adds photo to Gallery application.
    public void addPhotoToGallery() {
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);


Does not immediately update the media "database". I discovered that using the scanFile method of the MediaScannerConnection class:




Does cause an immediate update, so that requerying using ContentResolver now works. I used the code in this article:




To come up with the additional Delphi code required, which is in the attached test app. I've added some comments which should clarify the whys and hows.




Share this post

Link to post

Dave ,

just an idea, have you tried to access the file ( photo) in TakePhotoFromCameraActionDidFinishTaking but not for reading, may be try to get file info like size, date, permission ..etc, this might trigger something in the system to fluch the file, i know this accelerate fushing files on Windows with RDP connection connected with the same user that have an application writing small files over network.

Share this post

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this