5

好吧,我尝试了一些方法,但还没有奏效。更“正确”的方式是这样(我认为),我真的需要帮助,我快疯了!:)

public void setRingtone(){
        String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
        String fileName = nombreActual+".mp3";

        File newSoundFile = new File(baseDir, fileName);
        Uri mUri = Uri.parse("android.resource://com.genaut.ringtonelists/raw/"+sonidoActual);//sonidoActual (sound name  without .mp3)
        ContentResolver mCr = 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, nombreActual);
           values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
           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);

           RingtoneManager.setActualDefaultRingtoneUri(Main.this, RingtoneManager.TYPE_RINGTONE, newUri);

    Log.d("AbsolutePath: ", newSoundFile.getAbsolutePath());
    Log.d("DefaultUri: ", RingtoneManager.getActualDefaultRingtoneUri(Main.this, RingtoneManager.TYPE_RINGTONE).toString());
    }

我对清单有这个权限:

    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我尝试了一些代码,尝试混合一些步骤(复制文件,并设置库..)但没有奏效。我真的坚持这一点。使用此代码,我可以将我的声音添加到(设置 - 设备 - 音频配置文件 - “选择配置文件” - 语音通话铃声)但默认情况下未选择它,如果我选择他,他不会播放任何声音:(

我在 SD 上的任何位置都看不到声音文件,日志是:

08-05 03:20:22.362: D/AbsolutePath:(6277): /mnt/sdcard/Ding Ding Dong.mp3
08-05 03:20:22.364: D/DefaultUri:(6277): content://media/external/audio/media/15233

这个例子:我复制的“叮叮咚.mp3”文件和其他文件在sdcard上但大小为0!.

4

1 回答 1

2

尝试执行此代码,一切顺利

File k = new File(path, "mysong.mp3"); // path is a file to /sdcard/media/ringtone

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "My Song title");
values.put(MediaStore.MediaColumns.SIZE, 215454);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Madonna");
values.put(MediaStore.Audio.Media.DURATION, 230);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);

//Insert it into the database
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
Uri newUri = main.getContentResolver().insert(uri, values);

RingtoneManager.setActualDefaultRingtoneUri(
  myActivity,
  RingtoneManager.TYPE_RINGTONE,
  newUri
);
于 2012-08-05T04:43:47.587 回答