我用下面的code和接收FileNotFound异常。谁能解释这是为什么?我使用的模拟器测试,2.3.3。
私人无效setRingtone(){
byte []的缓冲区= NULL;
InputStream的FIN = getBaseContext()getResources()openRawResource(R.raw.custom_ringtone)。
INT大小= 0;
尝试 {
大小= fIn.available();
缓冲区=新的字节[尺寸]
fIn.read(缓冲液);
fIn.close();
}赶上(IOException异常E){
Log.d(跟踪,IO 1+ E);
}
字符串路径= Environment.getExternalStorageDirectory()getPath()+/媒体/铃声/。
字符串文件名=custom_ringtone.mp3;
布尔存在=(新文件(路径))存在();
如果(!存在){
新的文件(路径).mkdirs();
Log.d(跟踪,路径添加+路径);
}其他{
Log.d(跟踪,路径没有添加+路径);
}
FileOutputStream中保存;
尝试 {
保存=新的FileOutputStream(路径+文件名);
save.write(缓冲液);
save.flush();
save.close();
}赶上(FileNotFoundException异常E){
Log.d(跟踪,FileNotFound+ E);
}赶上(IOException异常E){
Log.d(跟踪,IO 2+ E);
}
sendBroadcast(新意图(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.parse(文件://+路径+文件名)));
文件k =新的文件(路径,文件名);
ContentValues值=新ContentValues();
values.put(MediaStore.MediaColumns.DATA,k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE,日程安排陈绮贞);
values.put(MediaStore.MediaColumns.MIME_TYPE,音频/ MP3);
values.put(MediaStore.Audio.Media.ARTISTOne2MM);
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(k.getAbsolutePath());
。getContentResolver()删除(URI,MediaStore.MediaColumns.DATA += \+ k.getAbsolutePath()+\,NULL);
乌里newUri = getContentResolver()插入(URI,值)。
RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this,RingtoneManager.TYPE_RINGTONE,newUri);
}
解决方案
记住,你需要声明一个permision写入外部SD卡。
就在这一行添加到您的manifest.xml下的<舱单>
标签:
<使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/>
I'm using the code below and receiving the "FileNotFound" exception. Can anyone explain why this is? I'm using the emulator to test, 2.3.3.
private void setRingtone(){
byte[] buffer = null;
InputStream fIn = getBaseContext().getResources().openRawResource(R.raw.custom_ringtone);
int size = 0;
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
Log.d("trace", "IO 1" + e);
}
String path = Environment.getExternalStorageDirectory().getPath()+"/media/ringtone/";
String filename = "custom_ringtone.mp3";
boolean exists = (new File(path)).exists();
if (!exists){
new File(path).mkdirs();
Log.d("trace", "path added" + path);
}else{
Log.d("trace", "path not added" + path);
}
FileOutputStream save;
try {
save = new FileOutputStream(path+filename);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
Log.d("trace", "FileNotFound" + e);
} catch (IOException e) {
Log.d("trace", "IO 2" + e);
}
sendBroadcast(new Intent(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, "Schedule Cheer");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "One2MM");
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);
//Insert it into the database
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);
}
解决方案
Remember that you need to declare a permision to write to the external SD Card.
Just add this line to your manifest.xml under the <manifest>
tag:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />