开发者

Android: How to make Save to SD card function work for both pre and post Android 2.2

开发者 https://www.devze.com 2023-02-06 14:15 出处:网络
Probably a very simple solution but I have very little experience thus far. How do I alter the following to work for both Android 2.2 (mnt/sdcard) and previous versions (/sdcard).

Probably a very simple solution but I have very little experience thus far. How do I alter the following to work for both Android 2.2 (mnt/sdcard) and previous versions (/sdcard).

Thanks in advance!

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;      }

//After user432209 comment I have this and it is working

File pathDir = Environment.getExternalStorageDirectory();

String path=pathDir+"/media/audio/notifications/"; String filename="sound1"+".ogg";


You probably don't want to save directly to SD card. I would recommend "asking the phone" where it wants you to save.

File externalDirectory = Environment.getExternalStorageDirectory();
0

精彩评论

暂无评论...
验证码 换一张
取 消