3 回答

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊
如果您創(chuàng)建一個(gè)包裝頂級(jí)目錄的File對(duì)象,您可以調(diào)用它的mkdirs()方法來(lái)構(gòu)建所有需要的目錄。就像是:
// create a File object for the parent directory
File wallpaperDirectory = new File("/sdcard/Wallpaper/");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
// create a File object for the output file
File outputFile = new File(wallpaperDirectory, filename);
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);
注意:使用Environment.getExternalStorageDirectory()來(lái)獲取“SD卡”目錄可能是明智之舉,因?yàn)槿绻謾C(jī)出現(xiàn)了除SD卡以外的其他東西(例如內(nèi)置閃存,則可能會(huì)更改)蘋果手機(jī))。無(wú)論哪種方式,您都應(yīng)該記住,您需要檢查以確保它實(shí)際存在,因?yàn)榭赡軙?huì)移除SD卡。
更新:自API級(jí)別4(1.6)起,您還必須請(qǐng)求許可。這樣的事情(在清單中)應(yīng)該有效:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

TA貢獻(xiàn)2039條經(jīng)驗(yàn) 獲得超8個(gè)贊
這對(duì)我有用。
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
在你的清單和下面的代碼中
public static boolean createDirIfNotExists(String path) { boolean ret = true; File file = new File(Environment.getExternalStorageDirectory(), path); if (!file.exists()) { if (!file.mkdirs()) { Log.e("TravellerLog :: ", "Problem creating Image folder"); ret = false; } } return ret;}
- 3 回答
- 0 關(guān)注
- 560 瀏覽
添加回答
舉報(bào)