'Adding metadata tags (like title, artist, album) to audio file not working (Android MediaStore)

I want to create an audio file and set the title, artist and album tags of the song using Android MediaStore. Creating the file works but unfortunately the tags don't seem to be set. What am I doing wrong?

My Code:

    private long createFile(Context context, String fileName, InputStream fileContent) throws IOException {
        Uri audioCollection = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.Audio.Media.DISPLAY_NAME, fileName);
        contentValues.put(MediaStore.Audio.Media.TITLE, "some title");
        contentValues.put(MediaStore.Audio.Media.ARTIST, "some artist");
        contentValues.put(MediaStore.Audio.Media.ALBUM, "some album");
        contentValues.put(MediaStore.Audio.Media.MIME_TYPE, "audio/mpeg");

        ContentResolver contentResolver = context.getContentResolver();
        Uri uri = contentResolver.insert(audioCollection, contentValues);

        FileOutputStream out = (FileOutputStream) contentResolver.openOutputStream(uri);
        FileChannel outChannel = out.getChannel();

        ReadableByteChannel inChannel = Channels.newChannel(fileContent);

        outChannel.transferFrom(inChannel, 0, Long.MAX_VALUE);

        out.close();

        long id = Long.parseLong(uri.getLastPathSegment());
        return id;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        AsyncTask.execute(() -> {
            Log.e(LOG_TAG, "STARTED");
            try {
                String songUrl = "https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3";
                InputStream in = new URL(songUrl).openStream();

                long id = createFile(getApplicationContext(), "songname", in);
                Log.e(LOG_TAG, "CREATED FILE WITH ID " + id);

            } catch (IOException e) {
                Log.e(LOG_TAG, "ERROR: " + e.getMessage());
            }
        });
    }


Solution 1:[1]

As far as I understand right now it's not possible to set ID3 tags using the MediaStore, it only saves the tags locally on the device, so the metadata is lost when moving the file to another device.

So in the end I created my own tag editor (for ID3v1). Sourcecode if anyone is interested: https://github.com/nh7dev/ID3v1TagEditorJava

ID3v2 is more complex: https://id3.org/id3v2.3.0

Solution 2:[2]

Interesting - And why you dont youse mp3tag f.e ?

I am looking for a possibility to find my music by custom tags on android. Would be nice with foobar mobile.

On hydrogenaudio I recieved the answer, that android at all will not index them. Perhaps you have an idea that helps ;-)

https://hydrogenaud.io/index.php?topic=122435.new#new

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nh7
Solution 2 reselty