2015-12-11 28 views
0

我已經嘗試了每個關於寫入到外部存儲的解決方案。從2天開始工作,但無法在Android Kitkat 4.2.2中編寫。我無能爲力。我的清單文件已經閱讀&寫入權限和寫入文件的代碼是:無法寫入到存儲在Android

// make a new file directory inside the "sdcard" folder 
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES), "imgcaptureapp"); 

    // if the directory does not exist 
    if (!mediaStorageDir.exists()) { 
     // if you cannot make this directory return 
     if (!mediaStorageDir.mkdirs()) { 
      return null; 
     } 
    } 

    // take the current timeStamp 
    String timeStamp = new SimpleDateFormat("dd-MM-yyyy_HH:mm:ss").format(new Date()); 
    File mediaFile; 
    // and make a media file: 
    mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg"); 

    return mediaFile; 

我mediaFile.canwrite();返回false。

+3

請勿在文件名中使用冒號。 – CommonsWare

+0

@CommonsWare完美的作品。真棒&非常感謝。 –

+1

相關:[Android中的文件名允許使用哪些字符?](https://stackoverflow.com/questions/2679699/what-c​​haracters-allowed-in-file-names-on-android)。不幸的是,這組有效的文件名字符取決於文件系統的類型,外部存儲目錄已打開。因此:保守。 – dhke

回答

1

冒號在許多文件系統中都是保留字符,包括Android的。總體而言,您使用的文件名相對於標點符號風格的字符而言更簡單,您將獲得更好的效果。

在這種情況下,請使用恰好不使用冒號的日期時間格式(例如,YYYYMMDD-HHMMSS)。

+0

你可能想限制一點。 ':'限制只適用於外部安裝的SD卡,通常FAT32作爲文件系統。 Android對其他地方的冒號非常滿意,例如在模擬的SD卡fs上(通常是ext4或yaffs)。 – dhke