0

可能是一个非常简单的解决方案,但到目前为止我的经验很少。如何更改以下内容以适用于 Android 2.2 (mnt/sdcard) 和以前的版本 (/sdcard)。

提前致谢!

String path="/sdcard/media/audio/notifications/";      
String filename="sound1"+".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, "sound1 Notification");         
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");         
values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");         
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);       
return true;      }

//在user432209评论后我有这个并且它正在工作

文件路径目录 = Environment.getExternalStorageDirectory();

字符串路径=pathDir+"/media/audio/notifications/"; 字符串文件名="sound1"+".ogg";

4

1 回答 1

1

您可能不想直接保存到 SD 卡。我建议“询问电话”它希望您保存的位置。

File externalDirectory = Environment.getExternalStorageDirectory();
于 2011-01-24T03:24:17.537 回答