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



所以这个功能以前是工作的,但我猜当我升级版本时,它不再工作了。

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

  • Android/数据/com.mypackagename/文件/xxx.mp3

然后我创建一个新的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创建铃声的当前状态有什么线索吗?

这是我的错误。我需要纠正我的Manifest中的一些事情来获得权限:

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

Ringtones外部根文件夹无法从基本WRITE_EXTERNAL_STORAGE权限访问。我们可以访问应用程序特定的外部文件夹&(链接).

甚至媒体商店也不允许你访问这个文件夹,所以从Android 11 &向前,您需要MANAGE_EXTERNAL_STORAGE权限,它会给您这个警告:

大多数应用程序不允许使用MANAGE_EXTERNAL_STORAGE。因为你需要向用户请求这个权限,而他可能会拒绝。

但如果你想做我想做的事,你就需要它。

确保您的应用程序通过以下方式请求权限:

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