Android Studio:如何将铃声/通知/闹钟音频文件写入存储并在设置中查看

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

所以这个功能以前是有效的,但我想当我升级版本时,它不再有效了。

我想动态创建一个音频文件(这是有效的),并将其复制到存储(这是有效的,它目前已复制到我的本地应用程序存储中:

然后我使用数据和元数据创建一个新的 ContentValues 并将其插入 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI

之后我设置铃声并启动铃声选择器来检查:

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);

但是铃声设置只是媒体ID,不是媒体名称,我在列表里找不到..

我虽然没有扫描媒体,所以我事先试过了:

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

我不太确定这有什么用,但没有用。

关于使用 Android Studio 创建铃声的当前状态的任何线索?

所以这是我的错误。我需要更正清单中的一些内容才能获得权限:

//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"/>

无法再通过基本 WRITE_EXTERNAL_STORAGE 权限访问 Ringtones 外部根文件夹。我们可以访问特定于应用程序的外部文件夹和其他文件夹 (link)。

即使媒体商店也不允许您访问此文件夹,因此从 Android 11 及以后,您需要 MANAGE_EXTERNAL_STORAGE 权限,即给你这个警告:

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..

但是如果你想做我想做的事,你将需要它..

确保您的应用通过以下方式请求许可:

// 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;
    }
}