0

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.

lolo.io
  • 770
  • 5
  • 22

1 Answers1

0

Extract from StorageUtils, found on this forum

     public static ArrayList<String> getExtSdCardPaths(Context mContext) {
    ArrayList<String> paths = new ArrayList<String>();
    File[] files = getExternalFilesDirs(mContext, "external");
    File firstFile = files[0];
    for (File file : files) {
        if (file != null && !file.equals(firstFile)) {
            int index = file.getAbsolutePath().lastIndexOf("/Android/data");
            if (index < 0) {
                Log.w("", "Unexpected external file dir: " + file.getAbsolutePath());
            }
            else {
                String path = file.getAbsolutePath().substring(0, index);
                try {
                    path = new File(path).getCanonicalPath();
                }
                catch (IOException e) {
                    // Keep non-canonical path.
                }
                paths.add(path);
            }
        }
    }
    return paths;
}
Theo
  • 2,012
  • 1
  • 16
  • 29
  • Thanks Theo for your help. I can add the images to the desired folder now. I updated my post. Now I am stuck when I try to make the images how up in the gallery. – lolo.io Feb 09 '21 at 19:26
  • I think you need to call the File Picker . have a look here: https://stackoverflow.com/questions/42288227/how-to-display-image-with-intent-action-view – Theo Feb 10 '21 at 19:42