FossifyOrg / Gallery

Browse your memories without any interruptions with this photo and video gallery
https://www.fossify.org
GNU General Public License v3.0
1.33k stars 41 forks source link

EXIF location metadata is removed from image when selected via ACTION_PICK #218

Open fabalexsie opened 2 months ago

fabalexsie commented 2 months ago

Checklist

Feature description

If a third party application wants to retrieve an image and you decide to use this app for selecting, the location metadata should not be deleted and should also be made available to the third party application.

This is the android file selection dialog from which you can select this Gallery app ("Galerie").

file selection dialog to choose an app

Why do you want this feature?

My specific scenario: I want to upload some of my pictures to my nextcloud and see them on a map. I've used the android app Memories to upload some photos. The selection process there is done via an ACTION_PICK Intent (i guess) which opens the native file selection dialog of android. There i choose this gallery app and select some pictures. When the pictures are returned to the Memories app, the location metadata field is empty. So I can't use this data in my nextcloud.

General use case Selecting the image from another app for example for uploading or sharing it, should also share the location metadata and should not modify the file.

Additional information

I've also analysed it for my specific scenario and written a small fix.

I've tested it with the Memories app and I've written a small app myself that "asks" for an image via the Intent.ACTION_PICK and reads the location from the EXIF in the result. For both of them, this works. I could fix the problem by copying the file to a cache folder and providing the public uri of that cached file instead of the original.

diff --git a/app/src/main/kotlin/org/fossify/gallery/activities/MainActivity.kt b/app/src/main/kotlin/org/fossify/gallery/activities/MainActivity.kt
index 834453471..e73d065ab 100644
--- a/app/src/main/kotlin/org/fossify/gallery/activities/MainActivity.kt
+++ b/app/src/main/kotlin/org/fossify/gallery/activities/MainActivity.kt
@@ -880,9 +880,15 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
     private fun fillIntentPath(resultData: Intent, resultIntent: Intent) {
         val data = resultData.data
         val path = if (data.toString().startsWith("/")) data.toString() else data!!.path
-        val uri = getFilePublicUri(File(path!!), BuildConfig.APPLICATION_ID)
-        val type = path.getMimeType()
-        resultIntent.setDataAndTypeAndNormalize(uri, type)
+
+        // copy the file to the cache directory to keep all exif data like location
+        val file = File(path!!)
+        val cacheFile = File(cacheDir, file.name)
+        file.copyTo(cacheFile, true)
+
+        val cacheUri = getFilePublicUri(cacheFile, BuildConfig.APPLICATION_ID)
+        val type = cacheFile.path.getMimeType()
+        resultIntent.setDataAndTypeAndNormalize(cacheUri, type)
         resultIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
     }

Limitations: I don't know, if this only works on my phone or if there is a better way to do this. I also think that this needs to be fixed when sharing a picture from this gallery app to another third-party app.

Tested with: Android 12 (MIUI 13.0.1), Fossify Gallery 1.1.3

Aga-C commented 2 months ago

It should be under the option, where the default is removing location data while sharing. I'd even add a setting to remove all EXIF data, as requested here for another sharing scenario (sharing from Gallery to other apps): https://github.com/FossifyOrg/Gallery/issues/185.

Even better, these two requests should be done at once.

kvibber commented 3 days ago

I wonder if this is the reason the gallery's built-in editor is stripping out all the EXIF data from my images, including the date it was taken. In many cases, I'm editing a photo for my own use, or to upload to iNaturalist, in which case I usually want to keep all the metadata including the location, or most of the metadata but not location.

Aga-C commented 3 days ago

@kvibber See: https://github.com/FossifyOrg/Gallery/issues/29.