简体   繁体   English

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

[英]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:我想动态创建一个音频文件(这是有效的),并将其复制到存储(这是有效的,它目前已复制到我的本地应用程序存储中:

  • Android/data/com.mypackagename/files/xxx.mp3 android/data/com.mypackagename/files/xxx.mp3

Then I create a new ContentValues with the data & metadata and insert it into MediaStore.Audio.Media.EXTERNAL_CONTENT_URI .然后我使用数据和元数据创建一个新的 ContentValues 并将其插入到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..但是铃声设置只是媒体的ID,不是媒体的名字,我在列表里也找不到。。

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?使用 Android Studio 创建铃声的当前状态有什么线索吗?

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.无法再通过基本 WRITE_EXTERNAL_STORAGE 权限访问铃声外部根文件夹。 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:即使是媒体商店也不允许您访问此文件夹,因此从 Android 11 及以后,您需要MANAGE_EXTERNAL_STORAGE权限,这会给您以下警告:

Most apps are not allowed to use MANAGE_EXTERNAL_STORAGE.大多数应用程序不允许使用 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM