Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android實現讀寫SD卡

Android實現讀寫SD卡

編輯:Android開發實例

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

   1. 獲取SD卡的根目錄

代碼如下:

String  sdCardRoot = Environment.getExternalStorageDirectory().getAbsolutePath();  

 2. 在SD卡上創建文件夾目錄

/** 
 * 在SD卡上創建目錄 
 */ 
public File createDirOnSDCard(String dir) 
{ 
  File dirFile = new File(sdCardRoot + File.separator + dir +File.separator); 
  Log.v("createDirOnSDCard", sdCardRoot + File.separator + dir +File.separator); 
  dirFile.mkdirs(); 
  return dirFile; 
} 

 3. 在SD卡上創建文件

/** 
 * 在SD卡上創建文件 
 */ 
public File createFileOnSDCard(String fileName, String dir) throws IOException 
{ 
  File file = new File(sdCardRoot + File.separator + dir + File.separator + fileName); 
  Log.v("createFileOnSDCard", sdCardRoot + File.separator + dir + File.separator + fileName); 
  file.createNewFile(); 
  return file; 
} 

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

/** 
 * 判斷SD卡上文件是否存在 
 */ 
public boolean isFileExist(String fileName, String path) 
{ 
  File file = new File(sdCardRoot + path + File.separator + fileName); 
  return file.exists(); 
} 

  5.將數據寫入到SD卡指定目錄文件

/* 寫入數據到SD卡中 
   */ 
  public File writeData2SDCard(String path, String fileName, InputStream data) 
  { 
    File file = null; 
    OutputStream output = null; 
     
    try { 
      createDirOnSDCard(path); //創建目錄 
      file = createFileOnSDCard(fileName, path); //創建文件 
      output = new FileOutputStream(file); 
      byte buffer[] = new byte[2*1024];     //每次寫2K數據 
      int temp; 
      while((temp = data.read(buffer)) != -1 ) 
      { 
        output.write(buffer,0,temp); 
      } 
      output.flush(); 
       
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
    finally{ 
      try { 
        output.close();  //關閉數據流操作 
      } catch (Exception e2) { 
        e2.printStackTrace(); 
      } 
    } 
     
    return file; 
  } 

   注意事項

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

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

詳情看這裡 http://www.jb51.net/article/34296.htm

注意:不直接進行讀出是為了防止打文件操作對內存的消耗,所以用輸入流讀到硬盤上。

public String readFile(String fileName) throws Exception{
    FileInputStream fis = context.openFileInput(fileName);
    byte[] bytes = new byte[fis.available()];
    fis.read(bytes);
    fis.close();
    return new String(bytes,"utf-8");
  }

當文件很大的時候,byte[]會占用很大的內存。

package cn.itcast.fileio.service;
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
import android.content.Context;
import android.os.Environment;
 
public class SdCardService {
  private Context ctx;
 
  public SdCardService(Context ctx) {
    this.ctx = ctx;
  }
 
  /**
   * 寫文件入skcard
   */
  public void writeToSdCard(String fileName, String cont) {
    try {
      // 判斷是否有掛載sdcard
      if (Environment.getExternalStorageState().equals(
          Environment.MEDIA_MOUNTED)) {
        // 得到sdcar文件目錄
        File dir = Environment.getExternalStorageDirectory();
        File file = new File(dir, "itcast.txt");
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(cont.getBytes());
        fos.close();
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * 讀SdCard中的文件
   */
  public String readSdCard(String fileName) {
    try {
      // 判斷是否有掛載sdcard
      if (Environment.getExternalStorageState().equals(
          Environment.MEDIA_MOUNTED)) {
        // 得到sdcar文件目錄
        File dir = Environment.getExternalStorageDirectory();
        File file = new File(dir, "itcast.txt");
        FileInputStream fis = new FileInputStream(file);
        return readIs2String(fis);
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
   
  /**
   * 將輸入流數據讀取到輸出流當中
   */
  private OutputStream readIs2Os(InputStream is ,OutputStream os){
    try {
      byte[] bytes = new byte[1024];
      int length = 0 ;
      while((length = is.read(bytes)) != -1){
        os.write(bytes, 0, length);
      }
      is.close();
      os.close();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    return os ;
  }
   
  /**
   * 將輸入流數據讀取到輸出流當中
   */
  public byte[] readIs2Bytes(InputStream is){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    readIs2Os(is,baos);
    return baos.toByteArray() ;
  }
   
  public String readIs2String(InputStream is){
    try {
      return new String(readIs2Bytes(is),"utf-8");
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    return null ;
  }
   
  public String readIs2String(String fileName){
    try {
      if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
        File dir = Environment.getExternalStorageDirectory();
        File file = new File(dir,fileName);
        FileInputStream is = new FileInputStream(file);
        return readIs2String(is);
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    return null ;
  }
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved