java - Android:如何使保存到 SD 卡功能适用于 Android 2.2 之前和之后

标签 java android sd-card android-2.2-froyo

可能是一个非常简单的解决方案,但到目前为止我的经验很少。如何更改以下内容以适用于 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 评论后我得到了这个并且它正在工作

文件路径Dir = Environment.getExternalStorageDirectory();

字符串路径=pathDir+"/media/audio/notifications/"; 字符串文件名=“声音1”+“.ogg”;

最佳答案

您可能不想直接保存到 SD 卡。我建议您“询问手机”它希望您将其保存在哪里。

File externalDirectory = Environment.getExternalStorageDirectory();

关于java - Android:如何使保存到 SD 卡功能适用于 Android 2.2 之前和之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4778283/

相关文章:

java - dependencyManagement 用于包含 <classifier> 和不包含 <classifier> 的依赖关系

android - 下载图像并将其存储在 ContentProvider 中

Android 打开或创建数据库

c - 不断向文件(SDCard)写入数据时,应该多久打开和关闭一次文件?

java - MVP Views 到底有多蠢?

Java不安全的内存分配限制

java - 在Java中初始化大量数组?

java - 如何区分显示屏右侧/左侧的点击?

android - Android:首次从命令行运行应用

Android:如何与microSD卡进行SPI通信