Search code examples
javaandroidmediastoreandroid-11

"audio/m4a" mime type not supported Android 11


What I am trying to do: Using Mediastore Api to download and retrieve audio from Sdcard.(Android 11)

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DISPLAY_NAME, "menuCategory");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/m4a");
values.put(MediaStore.MediaColumns.RELATIVE_PATH,Environment.DIRECTORY_DOWNLOADS+"/DownloadMyMusic");
values.put(MediaStore.Audio.Media.ARTIST, trackStoreDTO.getArtistName());
Uri uri = context.getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);     
OutputStream outputStream = context.getContentResolver().openOutputStream(uri);

I am getting error for line 3 stating: "audio/mp4" mime type not supported, I assumed I might be doing something wrong so I created a sample app and it works totally fine android 11, I have searched everything and totally clueless right now, any help will be appreciated, Thanks!


Solution

  • Not sure how it actually worked, but stumbled upon this and changed

    Uri uri = context.getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);     
    to Uri uri = context.getContentResolver().insert(MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY), values);
    

    and it's working, in case anyone get's stuck on the same.