无法在 Android-11 中以编程方式设置铃声/通知音

发布于 2025-01-13 06:59:49 字数 3878 浏览 0 评论 0原文

我已经知道这个问题已经被问过很多次并得到了回答,但这是不同的。我试图在 android-11 中定下基调,并通过互联网尝试了所有可能的解决方案。

我发现的最有用的问题和答案是 如何将文件设置为 Android 10 的铃声? 但我仍然无法实现。

问题是我能够将我的 rawfolder.mp3 文件保存到手机的本地存储中,但是当我尝试将其设置为音调时,我遇到了异常,并且无法设置它。我尝试将日志放在下面两行中,但所有这些都没有被执行。如果有人可以提供帮助,我们将不胜感激。谢谢。

getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_RINGTONE, newUri);

我已经授予了存储权限,并且运行时检查了所有权限。我还设置了 android:requestLegacyExternalStorage="true"

我的整个代码

InputStream fIn = getBaseContext().getResources().openRawResource(R.raw.ring);
    try {
        byte[] buffer = new byte[fIn.available()];
        fIn.read(buffer);
        fIn.close();
        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES) + "/Tone/";
        String filename = "Ring.mp3";

        if (!new File(path).exists()) {
            new File(path).mkdirs();
        }

        try {
            FileOutputStream save = new FileOutputStream(path + filename);
            save.write(buffer);
            save.flush();
            save.close();
            sendBroadcast(new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", Uri.parse("file://" + path + filename)));

            File k = new File(path, filename);
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, "RingTone");
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.MediaColumns.ARTIST, R.string.app_name);
            values.put(MediaStore.Audio.Media.IS_RINGTONE, Boolean.valueOf(true));
            values.put(MediaStore.Audio.Media.IS_NOTIFICATION, Boolean.valueOf(false));
            values.put(MediaStore.Audio.Media.IS_ALARM, Boolean.valueOf(false));
            values.put(MediaStore.Audio.Media.IS_MUSIC, Boolean.valueOf(false));

            Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
            getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
            Uri newUri = getContentResolver().insert(uri, values);
            RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_RINGTONE, newUri);
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e2) {
            e2.printStackTrace();
            return false;
        }
    } catch (IOException e3) {
        e3.printStackTrace();
        return false;
    }

我也尝试过另一种方法,但得到相同的结果,例如无法设置为铃声

Uri newUri = getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        try (OutputStream os = getContentResolver().openOutputStream(newUri)) {
            //copy your file from asset into os here
            int size = (int) f.length();
            byte[] bytes = new byte[size];
            try {
                BufferedInputStream buf = new BufferedInputStream(new FileInputStream(f));
                buf.read(bytes, 0, bytes.length);
                buf.close();

                os.write(bytes);
                os.close();
                os.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception ignored) {
            ignored.printStackTrace();
        }
    }

I already know this question has been asked many times and answered, But this is something different. I am trying to set a Tone in android-11 and tried all the possible solutions over the internet and SO.

The most helpful question and answer which I found is How to set a file as a ringtone for Android 10? but still, I couldn't make it happen.

The thing is I am able to save my raw folder.mp3 file to local storage in my phone but when I try to set this as a tone I am getting exceptions and I am unable to set it. I have tried putting log and all only this below 2 lines not getting executed. If anyone can help that would be much appreciated. Thank you.

getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_RINGTONE, newUri);

I have already given storage permission and also runtime checked about all the permission. Also i have set android:requestLegacyExternalStorage="true"

my whole code

InputStream fIn = getBaseContext().getResources().openRawResource(R.raw.ring);
    try {
        byte[] buffer = new byte[fIn.available()];
        fIn.read(buffer);
        fIn.close();
        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES) + "/Tone/";
        String filename = "Ring.mp3";

        if (!new File(path).exists()) {
            new File(path).mkdirs();
        }

        try {
            FileOutputStream save = new FileOutputStream(path + filename);
            save.write(buffer);
            save.flush();
            save.close();
            sendBroadcast(new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", Uri.parse("file://" + path + filename)));

            File k = new File(path, filename);
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, "RingTone");
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.MediaColumns.ARTIST, R.string.app_name);
            values.put(MediaStore.Audio.Media.IS_RINGTONE, Boolean.valueOf(true));
            values.put(MediaStore.Audio.Media.IS_NOTIFICATION, Boolean.valueOf(false));
            values.put(MediaStore.Audio.Media.IS_ALARM, Boolean.valueOf(false));
            values.put(MediaStore.Audio.Media.IS_MUSIC, Boolean.valueOf(false));

            Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
            getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
            Uri newUri = getContentResolver().insert(uri, values);
            RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_RINGTONE, newUri);
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e2) {
            e2.printStackTrace();
            return false;
        }
    } catch (IOException e3) {
        e3.printStackTrace();
        return false;
    }

Also i have tried with another method but getting same result like unable to set as ringtone

Uri newUri = getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        try (OutputStream os = getContentResolver().openOutputStream(newUri)) {
            //copy your file from asset into os here
            int size = (int) f.length();
            byte[] bytes = new byte[size];
            try {
                BufferedInputStream buf = new BufferedInputStream(new FileInputStream(f));
                buf.read(bytes, 0, bytes.length);
                buf.close();

                os.write(bytes);
                os.close();
                os.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception ignored) {
            ignored.printStackTrace();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文