Android手機儲存路徑
阿新 • • 發佈:2018-11-06
手機儲存
手機分為內部儲存和外部儲存
1、內部儲存
主要方法 | 路徑 |
---|---|
Environment.getDataDirectory() | /data |
Environment.getDownloadCacheDirectory() | /cache |
Environment.getRootDirectory() | /system |
這裡的內部儲存對應的就是手機QQ的手機記憶體,路徑為
Environment.getDataDirectory().getParentFile()
就是根目錄,這個目錄下還有一些私密的資料,比如:databases(資料庫)、shared_prefs(SharedPreferences)等
注:沒有root的手機不能開啟該資料夾
2、外部儲存
外部儲存又分為SD卡和擴充套件卡記憶體
(1)SD卡
獲取路徑方式是
Environment.getExternalStorageDirectory() /storage/sdcard0
方法 | 路徑 |
---|---|
Environment.getExternalStoragePublicDirectory(DIRECTORY_ALARMS) | /storage/sdcard0/Alarms |
Environment.getExternalStoragePublicDirectory(DIRECTORY_DCIM) | /storage/sdcard0/DCIM |
Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS) | /storage/sdcard0/Download |
Environment.getExternalStoragePublicDirectory(DIRECTORY_MOVIES) | /storage/sdcard0/Movies |
Environment.getExternalStoragePublicDirectory(DIRECTORY_MUSIC) | /storage/sdcard0/Music |
Environment.getExternalStoragePublicDirectory(DIRECTORY_NOTIFICATIONS) | /storage/sdcard0/Notifications |
Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES) | /storage/sdcard0/Pictures |
Environment.getExternalStoragePublicDirectory(DIRECTORY_PODCASTS) | /storage/sdcard0/Podcasts |
Environment.getExternalStoragePublicDirectory(DIRECTORY_RINGTONES) | /storage/sdcard0/Ringtones |
上面的九個方法對應的就是SD卡的九大公有目錄,Google官方建議我們資料應該儲存在私有目錄下,不建議儲存在公有目錄下或其他地方
那什麼是私有目錄呢?
就是在外部儲存的App的包名下,如:
/storage/emulated/0/Android/data/cwj.test(包名)/files/test
方法 | 路徑 |
---|---|
getExternalFilesDir() | /storage/emulated/0/Android/data/cwj.test(包名)/files/test |
getExternalCacheDir | /storage/emulated/0/Android/data/cwj.test(包名)/cache/test |
一般情況下有包名的路徑我們都是呼叫Context中的方法來獲得,沒有包名的路徑,我們直接呼叫Environment中的方法獲得。
(2)擴充套件卡記憶體
擴充套件記憶體就是我們插入的外接SD卡,獲取擴充套件記憶體的路徑有點難度,不過Google很牛逼,哈哈,程式碼如下:
private 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;
}
該方法主要是通過反射的方式使用在sdk中被 隱藏 的類 StroageVolume 中的方法getVolumeList(),獲取外部儲存的路徑。
路徑測試:
Log.e("cwj", "外接SD卡路徑 = " + getStoragePath(this));
Log.e("cwj", "內建SD卡路徑 = " + Environment.getExternalStorageDirectory().getAbsolutePath());
Log.e("cwj", "手機記憶體根目錄路徑 = " + Environment.getDataDirectory().getParentFile().getAbsolutePath());
image.png