在我的Android应用程序,我想从我的原
文件夹作为铃声设置的音频文件。
为此,我写了下面code,但它不工作。
请帮我解决这个问题。 谢谢你。
code:
字符串名称= best_song_ever.mp3;
文件newSoundFile =新的文件(/ SD卡/媒体/铃声,
myringtone.mp3);
乌里穆里= Uri.parse(android.resource://
+ context.getPackageName()+/生/+姓名);
ContentResolver的MCR = context.getContentResolver();
AssetFileDescriptor音效档;
尝试 {
音效档= mCr.openAssetFileDescriptor(穆里,R);
}赶上(FileNotFoundException异常E){
音效档= NULL;
}
尝试 {
byte []的READDATA =新的字节[1024];
的FileInputStream FIS = soundFile.createInputStream();
FileOutputStream中FOS =新的FileOutputStream(newSoundFile);
INT I = fis.read(READDATA);
而(ⅰ!= - 1){
fos.write(READDATA,0,I);
I = fis.read(READDATA);
}
fos.close();
}赶上(IOException异常IO){
}
ContentValues值=新ContentValues();
values.put(MediaStore.MediaColumns.DATA,
newSoundFile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE,我的手机铃声);
values.put(MediaStore.MediaColumns.MIME_TYPE,音频/ 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,真正的);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION,真正的);
values.put(MediaStore.Audio.Media.IS_ALARM,真正的);
values.put(MediaStore.Audio.Media.IS_MUSIC,假);
开放的我们的uri = MediaStore.Audio.Media
.getContentUriForPath(newSoundFile.getAbsolutePath());
乌里newUri = mCr.insert(URI,价值观);
尝试 {
RingtoneManager.setActualDefaultRingtoneUri(背景下,
RingtoneManager.TYPE_RINGTONE,newUri);
}赶上(的Throwable T){
}
Toast.makeText(上下文,名+被设置为铃声。,
Toast.LENGTH_LONG).show();
}
解决方案
以下code解决我的问题:
字符串名称=your_raw_audio_name;
档案文件=新的文件(Environment.getExternalStorageDirectory()
/ myRingtonFolder /音频/);
如果(!file.exists()){
file.mkdirs();
}
字符串路径= Environment.getExternalStorageDirectory()
.getAbsolutePath()+/ myRingtonFolder /音频/;
文件f =新的文件(路径+/+名.MP3);
乌里穆里= Uri.parse(android.resource://
+ context.getPackageName()+/生/+姓名);
ContentResolver的MCR = context.getContentResolver();
AssetFileDescriptor音效档;
尝试 {
音效档= mCr.openAssetFileDescriptor(穆里,R);
}赶上(FileNotFoundException异常E){
音效档= NULL;
}
尝试 {
byte []的READDATA =新的字节[1024];
的FileInputStream FIS = soundFile.createInputStream();
FileOutputStream中FOS =新的FileOutputStream(F);
INT I = fis.read(READDATA);
而(ⅰ!= - 1){
fos.write(READDATA,0,I);
I = fis.read(READDATA);
}
fos.close();
}赶上(IOException异常IO){
}
ContentValues值=新ContentValues();
values.put(MediaStore.MediaColumns.DATA,f.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE,姓名);
values.put(MediaStore.MediaColumns.MIME_TYPE,音频/ 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,真正的);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION,真正的);
values.put(MediaStore.Audio.Media.IS_ALARM,真正的);
values.put(MediaStore.Audio.Media.IS_MUSIC,真正的);
开放的我们的uri = MediaStore.Audio.Media.getContentUriForPath(F
.getAbsolutePath());
乌里newUri = mCr.insert(URI,价值观);
尝试 {
RingtoneManager.setActualDefaultRingtoneUri(背景下,
RingtoneManager.TYPE_RINGTONE,newUri);
Settings.System.putString(MCR,Settings.System.RINGTONE,
newUri.toString());
}赶上(的Throwable T){
}
In my android application, I want to set audio file from my raw
folder as a ringtone.
For that i wrote below code, but its not working.
Please help me to solve this issue. Thank you.
Code :
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();
}
解决方案
The following code solved my problem :
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) {
}