android - 在 Android 中将原始资源设置为铃声

标签 android android-contentprovider android-mediaplayer android-contentresolver mediastore

在我的 android 应用程序中,我想将我的 raw 文件夹中的音频文件设置为铃声。 为此,我写了下面的代码,但它不起作用。

请帮我解决这个问题。 谢谢。

代码:

String name =best_song_ever.mp3;
File newSoundFile = new File("/sdcard/media/ringtone",
                        "myringtone.mp3");
                Uri mUri = Uri.parse("android.resource://"
                        + context.getPackageName() + "/raw/" + name);
                ContentResolver mCr = context.getContentResolver();
                AssetFileDescriptor soundFile;
                try {
                    soundFile = mCr.openAssetFileDescriptor(mUri, "r");
                } catch (FileNotFoundException e) {
                    soundFile = null;
                }

            try {
                byte[] readData = new byte[1024];
                FileInputStream fis = soundFile.createInputStream();
                FileOutputStream fos = new FileOutputStream(newSoundFile);
                int i = fis.read(readData);

                while (i != -1) {
                    fos.write(readData, 0, i);
                    i = fis.read(readData);
                }

                fos.close();
            } catch (IOException io) {
            }
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA,
                    newSoundFile.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, "my ringtone");
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/oog");
            values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length());
            values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
            values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
            values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
            values.put(MediaStore.Audio.Media.IS_ALARM, true);
            values.put(MediaStore.Audio.Media.IS_MUSIC, false);

            Uri uri = MediaStore.Audio.Media
                    .getContentUriForPath(newSoundFile.getAbsolutePath());
            Uri newUri = mCr.insert(uri, values);

            try {
                RingtoneManager.setActualDefaultRingtoneUri(context,
                        RingtoneManager.TYPE_RINGTONE, newUri);
            } catch (Throwable t) {

            }

            Toast.makeText(context, name + " is set as ringtone.",
                    Toast.LENGTH_LONG).show();
        }

最佳答案

下面的代码解决了我的问题:

String name = "your_raw_audio_name";
File file = new File(Environment.getExternalStorageDirectory(),"/myRingtonFolder/Audio/");
if (!file.exists()) {
   file.mkdirs();
}

String path = Environment.getExternalStorageDirectory()
   .getAbsolutePath() + "/myRingtonFolder/Audio/";
File f = new File(path + "/", name + ".mp3");
Uri mUri = Uri.parse("android.resource://"
                    + context.getPackageName() + "/raw/" + name);
ContentResolver mCr = context.getContentResolver();
AssetFileDescriptor soundFile;
try {
    soundFile = mCr.openAssetFileDescriptor(mUri, "r");
} catch (FileNotFoundException e) {
    soundFile = null;
}

try {
    byte[] readData = new byte[1024];
    FileInputStream fis = soundFile.createInputStream();
    FileOutputStream fos = new FileOutputStream(f);
    int i = fis.read(readData);
    while (i != -1) {
        fos.write(readData, 0, i);
        i = fis.read(readData);
    }
    fos.close();
} catch (IOException io) {}

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, f.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, name);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.MediaColumns.SIZE, f.length());
values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, true);

Uri uri = MediaStore.Audio.Media.getContentUriForPath(f.getAbsolutePath());
Uri newUri = mCr.insert(uri, values);

try {
    RingtoneManager.setActualDefaultRingtoneUri(context,
        RingtoneManager.TYPE_RINGTONE, newUri);
    Settings.System.putString(mCr, Settings.System.RINGTONE,newUri.toString());
} catch (Throwable t) {}

关于android - 在 Android 中将原始资源设置为铃声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18100885/

相关文章:

android - kSOAP2 对双 ID 异常有点过于严格?

android - 不使用 AssetManager 从资源加载文件

android - JavaCV和FFmpegMediaMetadataRetriever:DuplicateFileException

Android TextView 网址

android - 在另一个应用程序中向我自己的 ContentProvider 请求读取权限

android - TextureView 中的 MediaPlayer 未按预期工作

java - Android MediaPlayer.OnBufferingUpdateListener - 缓冲内容的百分比为负

android - ContentUris.withAppendedId中 '_id'有什么用

android - 如何使用浏览器内容提供者?

java - Android 音乐播放器不播放选定的歌曲