好的,我尝试了一些方法都没有成功。更加“正确”的方式是这样的(我认为),我真的需要帮助,我快要疯掉了!
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" />
我尝试过一些代码,尝试混合一些步骤(复制文件,设置库...),但没有任何作用。我真的卡在这里了。使用此代码,我可以将我的声音添加到(Settings - Device - Audio profiles - "Select a profile" - Voice call ringtone),但它不是默认选择的,如果我选择它,他不会播放任何声音:(。
我在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
举例来说,我复制的"Ding Ding Dong.mp3"文件和其他文件都在sd卡上,但大小为0!