前言:
你對手機存儲到底了解多少?從安卓4.4以后在外置內存卡中有沒有存儲過東西。先上兩張圖片。
第一張圖片是酷我音樂下載設置的選項:
第二張是網易云音樂下載設置的選項:
從上面兩張圖可以看到外置卡的名字都是/storage/9016-4EF8
那么這個路徑是怎么拿到的呢?
任務:明白獲取到外置的存儲路徑。
我們只是講擴展卡內存,手機的外部存儲又分為SD卡和擴展卡內存,目前安卓旗艦手機的SD卡內存為128G,但是好多手機還是可以安裝擴展卡,大部分是在卡槽的地方。
想要獲取上面的路徑,我們可以用反射:
public static String getExtendedMemoryPath(Context mContext) {
StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
Class<?> storageVolumeClazz = null;
try {
storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
Method getPath = storageVolumeClazz.getMethod("getPath");
Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
Object result = getVolumeList.invoke(mStorageManager);
final int length = Array.getLength(result);
for (int i = 0; i < length; i++) {
Object storageVolumeElement = Array.get(result, i);
String path = (String) getPath.invoke(storageVolumeElement);
boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
if (removable) {
return path;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
上面的代碼就可以找到上面的路徑。
新的問題怎樣知道外置內存卡的大小呢?可用內存呢?還是上代碼
/**
* 根據路徑獲取內存狀態
*
* @param path
* @return
*/
private String getMemoryInfo(File path) {
// 獲得一個磁盤狀態對象
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize(); // 獲得一個扇區的大小
long totalBlocks = stat.getBlockCount(); // 獲得扇區的總數
long availableBlocks = stat.getAvailableBlocks(); // 獲得可用的扇區數量
// 總空間
String totalMemory = Formatter.formatFileSize(this, totalBlocks * blockSize);
// 可用空間
String availableMemory = Formatter.formatFileSize(this, availableBlocks * blockSize);
return "總空間: " + totalMemory + "可用空間: " + availableMemory;
}
這樣就輕松地看到外置存儲卡的存儲路徑以及外置卡的大小。
怎樣存儲到外部呢?
我們看到上面的圖存儲路徑低有規律的/storage/9016-4EF8/Android/data/包名/
都是這個開頭的。我們來驗證一下能不能存入。打開手機文件夾/Android/data/
目錄,發現沒有我們需要的包名的文件夾。根據大多數人的習慣一定會執行下面的代碼:
/**
* 根據文件路徑 遞歸創建文件
*
* @param file
*/
public static void createDipPath(String file) {
String parentFile = file.substring(0, file.lastIndexOf("/"));
File file1 = new File(file);
File parent = new File(parentFile);
if (!file1.exists()) {
parent.mkdirs();
try {
file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
但是當執行到parent.mkdirs();
這行代碼時,返回的是false,就是沒有成功。我們就手動建一個自己包名的文件夾吧!先驗證能不能不能存入:自己建立一個工程包名的文件夾,存入一個簡單的action.txt文件發現是可以的。那么問題來了總不能讓用戶自己去新建一個文件夾吧。問題還是有解決辦法的。
context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
判斷一下,我們工程包名的文件夾是否存在,如果不存在就可以執行上面的代碼,就會新建工程包名文件夾。
問題:那么安卓4.3及以下的版本呢??
上面的方法好使么?試試就知道!!!現在就要處理掛在的問題了。再4.3~4.0之間的版本大部分都是可以在加入一張位置SD卡的。手機也會有4G或者是8G的自帶擴展存儲。那么問題來了。這兩個怎么區分???
1.外置SD卡是否掛載和路徑地址:外置sd卡是否掛載
public static boolean isSDCardMounted() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
外置SD卡的路徑:
//外置sd卡路徑
String sdcardPath = System.getenv("EXTERNAL_STORAGE");
//外置sd卡路徑
String sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
先判斷是否掛載SD卡。如果SD卡存在才能使用外置SD卡。
2.手機自帶的擴展存儲:路徑:
//手機自帶的擴展存儲路徑
String extSdcardPath = System.getenv("SECONDARY_STORAGE");
這樣就基本可以兼容4.0以上的安卓版本了。