在參考質疑我可以在模擬器中使用手機內存而不是虛擬SD卡下載文件嗎?
Can anyone explain the File() parameters used to download file in android?
我能做到這一點,而無需創建虛擬SD卡。有什麼辦法將文件保存在手機內存中?如果可以不使用虛擬SD卡,那麼該怎麼做?
在參考質疑我可以在模擬器中使用手機內存而不是虛擬SD卡下載文件嗎?
Can anyone explain the File() parameters used to download file in android?
我能做到這一點,而無需創建虛擬SD卡。有什麼辦法將文件保存在手機內存中?如果可以不使用虛擬SD卡,那麼該怎麼做?
你的問題有點不清楚你想使用哪一個,所以這裏都是。
像mbaird說的,你可以使用 Context.openFileOutput()
輕鬆地將文件保存到手機的內部存儲。例如:
// Create file on internal storage and open it for writing
FileOutputStream fileOut;
try {
fileOut = openFileOutput(userId +".ics", Context.MODE_PRIVATE);
} catch (IOException e) {
// Error handling
}
// Write to output stream as usual
// ...
這將產生對手機的內部存儲新的文件,在這樣的路徑:
/data/data/com.example.yourpackagename/files/123456.ics
。
只有您的應用程序可以讀取此文件;其他人將無法讀取此文件,就像它在SD卡上一樣。
如果你想將文件保存到SD卡上,你需要的東西是這樣的:
if (Environment.getExternalStorageState() != Environment.MEDIA_MOUNTED) {
// SD card is not available
return;
}
File root = Environment.getExternalStorageDirectory() +"/myapp/";
File newFile = new File(root, userId +".ics");
FileOutputStream fileOut = new FileOutputStream(newFile);
// Write to output stream as usual
// ...
正如你所看到的,你不能依靠一個SD卡作爲寫作存在並且可用上到任何給定的時間點。這可能是以下幾個原因:
克里斯托弗,你是明星,非常感謝!我要去嘗試 歡呼 – rob 2010-01-28 17:04:55
嗨克里斯托弗, 我試圖使文件和讀取它,但得到這些警告,因爲我在跟蹤目錄下載WARN /System.err的(223):java.net.SocketException異常:權限被拒絕(也許缺少INTERNET權限) WARN/System。err(223):java.io.FileNotFoundException:/data/data/com.example.helloandroid/files/345hde.ics 有何建議? – rob 2010-01-29 13:24:10
WARN/NotificationService(53):java.io.IOException:setDataSource失敗:狀態= 0x80000000 – rob 2010-01-29 13:28:39
如果你做Context.openFileOutput()並給它一個像「myfile.txt」這樣沒有路徑的文件名,它應該在你的應用程序的數據目錄中創建文件,而不是SD卡。
有沒有你不想要創建你的模擬器的虛擬SD卡的一個原因?它需要2秒。 – 2010-01-27 16:35:35
請記住,手機的內存可能很小(手機之間有所不同)。 – 2010-01-27 16:45:22
那麼我的項目規範是這樣的,我可以改變它,如果它不允許這樣做與手機內存 – rob 2010-01-27 16:57:06