2010-07-08 220 views
5

我正在寫一些文件到SD卡。我的android版本是2.1。我沒有在清單文件中設置任何權限,但它允許我寫入SD卡。寫入SD卡

請讓我知道爲什麼會有這種行爲。清單控制中只有WRITE_EXTERNAL_STORAGE寫入SD卡或我需要執行的任何其他配置。

感謝 Gururaja B○

回答

2

你與API級別3或更低編制?該權限已添加到API級別4(Android 1.6)中,因此所有使用API​​級別3(與仍然運行1.5的手機兼容)編譯的應用程序都將自動獲得該權限。

+0

我使用Eclair代碼庫來構建系統映像,它的API級別大於3.是否需要修改除檢查API級別之外的任何其他配置? – 2010-07-08 08:52:05

0

我已經創建了一個utils方法來檢查設備上是否有可用的SD卡,並在設備上獲取SD卡路徑(如果有)。

您可以將2個方法的波紋管複製到您需要的項目類中。在返回的路徑上創建項目的文件夾並在其上寫入數據。就這樣。

public String isRemovableSDCardAvailable() { 
    final String FLAG = "mnt"; 
    final String SECONDARY_STORAGE = System.getenv("SECONDARY_STORAGE"); 
    final String EXTERNAL_STORAGE_DOCOMO = System.getenv("EXTERNAL_STORAGE_DOCOMO"); 
    final String EXTERNAL_SDCARD_STORAGE = System.getenv("EXTERNAL_SDCARD_STORAGE"); 
    final String EXTERNAL_SD_STORAGE = System.getenv("EXTERNAL_SD_STORAGE"); 
    final String EXTERNAL_STORAGE = System.getenv("EXTERNAL_STORAGE"); 

    Map<Integer, String> listEnvironmentVariableStoreSDCardRootDirectory = new HashMap<Integer, String>(); 
    listEnvironmentVariableStoreSDCardRootDirectory.put(0, SECONDARY_STORAGE); 
    listEnvironmentVariableStoreSDCardRootDirectory.put(1, EXTERNAL_STORAGE_DOCOMO); 
    listEnvironmentVariableStoreSDCardRootDirectory.put(2, EXTERNAL_SDCARD_STORAGE); 
    listEnvironmentVariableStoreSDCardRootDirectory.put(3, EXTERNAL_SD_STORAGE); 
    listEnvironmentVariableStoreSDCardRootDirectory.put(4, EXTERNAL_STORAGE); 

    File externalStorageList[] = null; 
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { 
     externalStorageList = getContext().getExternalFilesDirs(null); 
    } 
    String directory = null; 
    int size = listEnvironmentVariableStoreSDCardRootDirectory.size(); 
    for (int i = 0; i < size; i++) { 
     if (externalStorageList != null && externalStorageList.length > 1 && externalStorageList[1] != null) 
      directory = externalStorageList[1].getAbsolutePath(); 
     else 
      directory = listEnvironmentVariableStoreSDCardRootDirectory.get(i); 

     directory = canCreateFile(directory); 
     if (directory != null && directory.length() != 0) { 
      if (i == size - 1) { 
       if (directory.contains(FLAG)) { 
        Log.e(getClass().getSimpleName(), "SD Card's directory: " + directory); 
        return directory; 
       } else { 
        return null; 
       } 
      } 
      Log.e(getClass().getSimpleName(), "SD Card's directory: " + directory); 
      return directory; 
     } 
    } 
    return null; 
} 

/** 
* Check if can create file on given directory. Use this enclose with method 
* {@link BeginScreenFragement#isRemovableSDCardAvailable()} to check sd 
* card is available on device or not. 
* 
* @param directory 
* @return 
*/ 
public String canCreateFile(String directory) { 
    final String FILE_DIR = directory + File.separator + "hoang.txt"; 
    File tempFlie = null; 
    try { 
     tempFlie = new File(FILE_DIR); 
     FileOutputStream fos = new FileOutputStream(tempFlie); 
     fos.write(new byte[1024]); 
     fos.flush(); 
     fos.close(); 
     Log.e(getClass().getSimpleName(), "Can write file on this directory: " + FILE_DIR); 
    } catch (Exception e) { 
     Log.e(getClass().getSimpleName(), "Write file error: " + e.getMessage()); 
     return null; 
    } finally { 
     if (tempFlie != null && tempFlie.exists() && tempFlie.isFile()) { 
      // tempFlie.delete(); 
      tempFlie = null; 
     } 
    } 
    return directory; 
}