javaandroidandroid-studiomobileringtone

Android Studio: How to write a ringtone / notification / alarm audio file to storage and see it in the settings


So this functionality was working previously, but I guess somewhere when I upgraded versions, it does not anymore.

I want to create dynamically an audio file (this is working), and copy it to the storage (this is working, it is currently copied to my local app storage :

Then I create a new ContentValues with the data & metadata and insert it into MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.

After that I set ringtone and launch ringtone picker to check:

RingtoneManager.setActualDefaultRingtoneUri(_instance, RingtoneManager.TYPE_RINGTONE, newUri);
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, newUri);                  
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, newUri);
startActivityForResult(intent, 1);

But the ringtone set is only the ID of the media, not the name, and I can't find it in the list..

I though the Media wasn't scanned, so i tried this beforehand:

Intent scanFileIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri);
sendBroadcast(scanFileIntent);

I'm not really sure what this does, but it didn't helped.

Any clues what's going on with the current status of creating ringtone with Android Studio ?


Solution

  • So here was my error. I needed to correct some things in my Manifest to get the rights permissions:

    //Without this folders will be inaccessible in Android-11 and above devices
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
    
    //Without this entry storage-permission entry will not be visible under app-info permissions list Android-10 and below
    <uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="29"
    tools:ignore="ScopedStorage"/>
    
    //Without this entry the folders will remain in-accessible in Android-10, even if WRITE_EXTERNAL_STORAGE as above is present.
    <application
        android:requestLegacyExternalStorage="true"/>
    

    The Ringtones external root folder is not accessible from basic WRITE_EXTERNAL_STORAGE permissions anymore. We have access to app specific external folder, & others (link).

    Even the Media store does not give you access to this folder, so from Android 11 & forward, you need the MANAGE_EXTERNAL_STORAGE permission, that gives you this warning:

    Most apps are not allowed to use MANAGE_EXTERNAL_STORAGE. Because you need to ask for this permission to the user, and he might refuse..

    But if you want to do what I wanted to do, you'll need it..

    Be sure that your app ask for the permission through:

    // permission: Manifest.permission.WRITE_EXTERNAL_STORAGE
    // permission_id: 1
    public Boolean checkPermission(String permission, Integer permission_id) {
        if (ContextCompat.checkSelfPermission(_instance, permission) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(_instance, new String[]{permission}, permission_id);
            return false;
        } else {
            return true;
        }
    }