Android: 写文件到SD卡

来源:互联网 发布:java string类split 编辑:程序博客网 时间:2024/06/10 09:01

Android: 写文件到SD卡

Android 
考虑到SD卡可能没有被mount,或者其他各种情况,操作SD卡上的文件总需要各种状态的判断。主要是使用Environment类里的一些接口进行判断: 
Java代码  收藏代码
  1. private void writeFileToSD() {  
  2.     String sdStatus = Environment.getExternalStorageState();  
  3.     if(!sdStatus.equals(Environment.MEDIA_MOUNTED)) {  
  4.         Log.d("TestFile""SD card is not avaiable/writeable right now.");  
  5.         return;  
  6.     }  
  7.     try {  
  8.         String pathName="/sdcard/test/";  
  9.         String fileName="file.txt";  
  10.         File path = new File(pathName);  
  11.         File file = new File(pathName + fileName);  
  12.         if( !path.exists()) {  
  13.             Log.d("TestFile""Create the path:" + pathName);  
  14.             path.mkdir();  
  15.         }  
  16.         if( !file.exists()) {  
  17.             Log.d("TestFile""Create the file:" + fileName);  
  18.             file.createNewFile();  
  19.         }  
  20.         FileOutputStream stream = new FileOutputStream(file);  
  21.         String s = "this is a test string writing to file.";  
  22.         byte[] buf = s.getBytes();  
  23.         stream.write(buf);            
  24.         stream.close();  
  25.           
  26.     } catch(Exception e) {  
  27.         Log.e("TestFile""Error on writeFilToSD.");  
  28.         e.printStackTrace();  
  29.     }  
  30. }  

需要加入权限: 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 


看文档说,可以使用Context.getExternalFilesDir来取得一个特殊的文件夹,该文件夹对USER不可见,最重要的是:当系统卸载该程序时,会自动删除该目录下的文件。 

如果不需要往SD卡上写文件,可以直接用以下简单代码: 
Java代码  收藏代码
  1. private void writeFile() {  
  2.     try {  
  3.         FileOutputStream stream = openFileOutput("testfile.txt", Context.MODE_WORLD_WRITEABLE);  
  4.         String s = "this is a test string writing to file.";  
  5.         byte[] buf = s.getBytes();  
  6.         stream.write(buf);  
  7.         stream.close();  
  8.     }  
  9.     catch (FileNotFoundException e) {  
  10.         Log.d("TestFile""File not found.");  
  11.     }  
  12.     catch (IOException e) {  
  13.         Log.d("TestFile""File write error.");  
  14.     }  
  15. }  

该文件会被放置于data/data/your_app_package_name/files下。 


值得注意的是,我们可以在程序运行期间动态检查SD卡是否可用。大致就是通过注册BroadcastReceiver实现,这个官方文档里有提到: 

Java代码  收藏代码
  1. void startWatchingExternalStorage() {      
  2.     mExternalStorageReceiver = new BroadcastReceiver()   
  3.     {          
  4.         @Override          
  5.         public void onReceive(Context context, Intent intent) {              
  6.             Log.i("test""Storage: " + intent.getData());              
  7.             updateExternalStorageState();          
  8.         }      
  9.     };      
  10.     IntentFilter filter = new IntentFilter();      
  11.     filter.addAction(Intent.ACTION_MEDIA_MOUNTED);      
  12.     filter.addAction(Intent.ACTION_MEDIA_REMOVED);      
  13.     registerReceiver(mExternalStorageReceiver, filter);      
  14.     updateExternalStorageState();  
  15. }  
  16. void stopWatchingExternalStorage() {      
  17.     unregisterReceiver(mExternalStorageReceiver);  
  18. }  
0 0
原创粉丝点击