Android入門開發之SD卡讀寫操作

來源:互聯網
上載者:User

標籤:

 SD卡的讀寫是我們在開發android 應用程式過程中最常見的操作。下面介紹SD卡的讀寫操作方式:

   

   1. 擷取SD卡的根目錄

 

[java] view plaincopy
  1. String  sdCardRoot = Environment.getExternalStorageDirectory().getAbsolutePath();   



 

 

   2. 在SD卡上建立檔案夾目錄

 

[java] view plaincopy
  1. /** 
  2.  * 在SD卡上建立目錄 
  3.  */  
  4. public File createDirOnSDCard(String dir)  
  5. {  
  6.     File dirFile = new File(sdCardRoot + File.separator + dir +File.separator);  
  7.     Log.v("createDirOnSDCard", sdCardRoot + File.separator + dir +File.separator);  
  8.     dirFile.mkdirs();  
  9.     return dirFile;  
  10. }  

 

 


    3. 在SD卡上建立檔案

 

[java] view plaincopy
  1. /** 
  2.  * 在SD卡上建立檔案 
  3.  */  
  4. public File createFileOnSDCard(String fileName, String dir) throws IOException  
  5. {  
  6.     File file = new File(sdCardRoot + File.separator + dir + File.separator + fileName);  
  7.     Log.v("createFileOnSDCard", sdCardRoot + File.separator + dir + File.separator + fileName);  
  8.     file.createNewFile();  
  9.     return file;  
  10. }  

 

 


      4.判斷檔案是否存在於SD卡的某個目錄

  

[java] view plaincopy
  1. /** 
  2.  * 判斷SD卡上檔案是否存在 
  3.  */  
  4. public boolean isFileExist(String fileName, String path)  
  5. {  
  6.     File file = new File(sdCardRoot + path + File.separator + fileName);  
  7.     return file.exists();  
  8. }  

 

 

 

       5.將資料寫入到SD卡指定目錄檔案

 

[java] view plaincopy
  1. <span style="white-space:pre">  </span>/** 
  2.      * 寫入資料到SD卡中 
  3.      */  
  4.     public File writeData2SDCard(String path, String fileName, InputStream data)  
  5.     {  
  6.         File file = null;  
  7.         OutputStream output = null;  
  8.           
  9.         try {  
  10.             createDirOnSDCard(path);  //建立目錄  
  11.             file = createFileOnSDCard(fileName, path);  //建立檔案  
  12.             output = new FileOutputStream(file);  
  13.             byte buffer[] = new byte[2*1024];          //每次寫2K資料  
  14.             int temp;  
  15.             while((temp = data.read(buffer)) != -1 )  
  16.             {  
  17.                 output.write(buffer,0,temp);  
  18.             }  
  19.             output.flush();  
  20.               
  21.         } catch (Exception e) {  
  22.             e.printStackTrace();  
  23.         }  
  24.         finally{  
  25.             try {  
  26.                 output.close();    //關閉資料流操作  
  27.             } catch (Exception e2) {  
  28.                 e2.printStackTrace();  
  29.             }  
  30.         }  
  31.           
  32.         return file;  
  33.     }  

 

    one more important thing:

      對SD卡的操作,必須要申請許可權:    

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 

轉自:http://blog.csdn.net/newjerryj/article/details/8829179

Android入門開發之SD卡讀寫操作(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.