12

I want to store my mp3 files recorded with my app to the Music folder with MediaStore for the new "Scoped Storage" of Android 10.
This method works good, but the files are named with timestamp (e.g. 1576253519449), without .mp3 extension.
If I add the extension manually with file manager, the file is recorded correctly.
How i can use fileName + ".mp3" to name the files?

String fileName; //Obtained by intent
MediaRecorder audioRecorder;
Uri audiouri;
ParcelFileDescriptor file;

private void startRecording() throws IOException {
    ContentValues values = new ContentValues(4);
    values.put(MediaStore.Audio.Media.TITLE, fileName);
    values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (System.currentTimeMillis() / 1000));
    values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/mp3");
    values.put(MediaStore.Audio.Media.RELATIVE_PATH, "Music/Recordings/");

    audiouri = getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);
    file = getContentResolver().openFileDescriptor(audiouri, "w");

    if (file != null) {
        audioRecorder = new MediaRecorder();
        audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        audioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        audioRecorder.setOutputFile(file.getFileDescriptor());
        audioRecorder.setAudioChannels(1);
        audioRecorder.prepare();
        audioRecorder.start();
    }
}

enter image description here

And another question: MediaStore.Audio.Media.RELATIVE_PATH is only for Android 10. How to maintain retrocompatibility with older releases?

EDIT:
For retrocompatibility, you can just remove values.put(MediaStore.Audio.Media.RELATIVE_PATH, "Music/Recordings/");: the files will be saved directly in Music directory.
Or use if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) to add the subdirectory only on Android 10.

devpelux
  • 2,492
  • 3
  • 18
  • 38
  • 4
    Use DISPLAY_NAME. – blackapps Dec 13 '19 at 17:41
  • Thanks, it works like a charm. – devpelux Dec 13 '19 at 17:51
  • @blackapps I'm wondering how to do the same for devices prior to Android Q using the media store API? – Hossam Hassan May 10 '20 at 23:20
  • Then use .DATA column. – blackapps May 11 '20 at 08:52
  • @FirestormXYZ, how can I create new directory in android Q just like whatsapp is doing like i want path should be **.../0/AudioRecording**. Here **AudioRecording** is a folder where i want to store my recording files. – OMI HARISHCHANDRA MEHTA Jul 09 '20 at 05:33
  • 1
    @OMIHARISHCHANDRAMEHTA For Android 10 and 11, you can add android:requestLegacyExternalStorage="true" to your element in the manifest: https://stackoverflow.com/a/58379655/3605220 But start to save your recordings inside applications dedicated folder, because in future releases of android you won't be able to create folders anymore, even with this legacy method. Neither whatsapp won't be able to do it anymore! – devpelux Oct 16 '20 at 23:51

1 Answers1

3

As @blackapps suggested, can be used the DISPLAY_NAME property.

devpelux
  • 2,492
  • 3
  • 18
  • 38