Android中的Serializable、Parcelable">Android中的Serializable、Parcelable
834
2022-05-29
使用時只需先判斷SDCard當前的狀態然后取得SdCard的目錄即可(見源代碼)
//SDcard 操作
ublic void SDCardTest() {
// 獲取擴展SD卡設備狀態
String sDStateString = android.os.Environment.getExternalStorageState();
// 擁有可讀可寫權限
if (sDStateString.equals(android.os.Environment.MEDIA_MOUNTED)) {
try {
// 獲取擴展存儲設備的文件目錄
File SDFile = android.os.Environment
.getExternalStorageDirectory();
// 打開文件
File myFile = new File(SDFile.getAbsolutePath()
+ File.separator + "MyFile.txt");
// 判斷是否存在,不存在則創建
if (!myFile.exists()) {
myFile.createNewFile();
}
// 寫數據
String szOutText = "Hello, World!";
FileOutputStream outputStream = new FileOutputStream(myFile);
outputStream.write(szOutText.getBytes());
outputStream.close();
} catch (Exception e) {
// TODO: handle exception
}// end of try
}// end of if(MEDIA_MOUNTED)
// 擁有只讀權限
else if (sDStateString
.endsWith(android.os.Environment.MEDIA_MOUNTED_READ_ONLY)) {
// 獲取擴展存儲設備的文件目錄
File SDFile = android.os.Environment.getExternalStorageDirectory();
// 創建一個文件
File myFile = new File(SDFile.getAbsolutePath() + File.separator
+ "MyFile.txt");
// 判斷文件是否存在
if (myFile.exists()) {
try {
// 讀數據
FileInputStream inputStream = new FileInputStream(myFile);
byte[] buffer = new byte[1024];
inputStream.read(buffer);
inputStream.close();
} catch (Exception e) {
// TODO: handle exception
}// end of try
}// end of if(myFile)
}// end of if(MEDIA_MOUNTED_READ_ONLY)
// end of func
解釋 : 執行一個由該對象所引用的文件系統雷斯塔特.(Google翻譯)
想計算SDCard大小和使用情況時, 只需要得到SD卡總共擁有的Block數或是剩余沒用的Block數,再乘以每個Block的大小就是相應的容量大小了單位byte.(見代碼)
public void SDCardSizeTest() {
// 取得SDCard當前的狀態
String sDcString = android.os.Environment.getExternalStorageState();
if (sDcString.equals(android.os.Environment.MEDIA_MOUNTED)) {
// 取得sdcard文件路徑
File pathFile = android.os.Environment
.getExternalStorageDirectory();
android.os.StatFs statfs = new android.os.StatFs(pathFile.getPath());
// 獲取SDCard上BLOCK總數
long nTotalBlocks = statfs.getBlockCount();
// 獲取SDCard上每個block的SIZE
long nBlocSize = statfs.getBlockSize();
// 獲取可供程序使用的Block的數量
long nAvailaBlock = statfs.getAvailableBlocks();
// 獲取剩下的所有Block的數量(包括預留的一般程序無法使用的塊)
long nFreeBlock = statfs.getFreeBlocks();
// 計算SDCard 總容量大小MB
long nSDTotalSize = nTotalBlocks * nBlocSize / 1024 / 1024;
// 計算 SDCard 剩余大小MB
long nSDFreeSize = nAvailaBlock * nBlocSize / 1024 / 1024;
}// end of if
// end of func
Android
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。