I am working with a native library that I have no control over. This native library creates business related media files and requires a filepath to know where to store the file it creates.
I only have access to this type of function : nativeCreatePicture(filepath: String).
I can't get it to work with Android R and MediaStore. As far as I understand, MediaStore works better with URIs. The only bit of workaround I came up with was creating the file in context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), and then copying this file while inserting the output of the outputstream in the mediastore. This is not optimal as this creates the same data twice.
Can I insert files in getExternalFilesDir in the MediaStore ? Or how can I insert a file in the MediaStore and still filling its data within the native lib I use ?
Thanks guys !
UPDATE :
I can save the image in a public folder without using MediaStore (by providing the absolute path to the native lib). Then I can access it with contentResolver and have its Uri. I want the images to show up in the Gallery.
I managed to get it to partially work, using this code :
fun updateMediaStore(context: Context, filename: String, absolutePath: String) {
val values = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, filename)
put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/CustomDir")
}
context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
MediaScannerConnection.scanFile(context, arrayOf(absolutePath), null) { path, uri ->
Log.i("ExternalStorage", "Scanned $path")
Log.i("ExternalStorage", "-> uri=$uri")
}
}
But it randomly works. I test it with 4 successive calls with 4 different newly created images. Sometimes 1 gets added to the gallery, sometimes 2, sometimes 3, rarely 4. I also tested to make only one call to MediaScannerConnection.scanFile with an array of the 4 images, but with no more success than before.
If anyone know how I can do this I'd appreciate help.