现在我正在使用它:
String path="/sdcard/media/audio/ringtones/";
String filename=soundname+".ogg";
boolean exists = (new File(path)).exists();
if (!exists){new File(path).mkdirs();}
FileOutputStream save;
try {
save = new FileOutputStream(path+filename);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
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, soundname);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
values.put(MediaStore.Audio.Media.ARTIST, "...");
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
this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
String i = "Saved as Ringtone.";
Toast.makeText(getApplicationContext(), i,
Toast.LENGTH_LONG).show();
return true;
}
但是eclipse说我应该使用:
Environment.getExternalStorageDirectory().getPath()
我尝试添加它,但现在它不会保存,我不知道该怎么做。有人可以帮我实现这个吗?
答案 0 :(得分:6)
没关系。
但你可以添加这一行:
@SuppressLint("SdCardPath")
以上
String path="/sdcard/media/audio/ringtones/";
(或)只需更改
String path="/sdcard/media/audio/ringtones/";
为:
String path=android.os.Environment.getExternalStorageDirectory().getPath() + "/media/audio/ringtones/";
答案 1 :(得分:1)
你可以通过抑制它来避免这种情况:
@SuppressLint("SdCardPath")
但您应该使用Environment.getExternalStorageDirectory()
喜欢
String PathToSdcard = Evironment.getExternalStorageDirectory().getPath()+"/media/audio/ringtone"
答案 2 :(得分:0)
最好使用Environment
,因为您无法确定外部存储路径是否为"/sdcard/"
。尝试使用
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES)
然后进行一些重构:
if (!dir.exists()) dir.mkdirs();
File file = new File(dir, filename);
save = new FileOutputStream(file);