Android 存储路径

手机存储:内部存储,外部存储

内部存储

内部存储是指手机的存储,Environment.getDataDirectory().getParentFile()为根目录,这个目录下还有一些私密的数据,比如:databases(数据库)、shared_prefs(SharedPreferences)等,私密数据需要有root权限才能打开

主要方法 路径
Environment.getDataDirectory() /data
Environment.getDownloadCacheDirectory() /cache
Environment.getRootDirectory() /system

外部存储

外部存储又分为SD卡和扩展卡内存

  • 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

第二个开始之后的就是9大公有目录,通常自己的数据不建议放置在公有目录,建议放在私有目录,私有目录就是自己的app包名下,如:

方法 路径
getExternalFilesDir() /storage/emulated/0/Android/data/com.test(包名)/files/test
getExternalCacheDir() /storage/emulated/0/Android/data/com.test(包名)/cache/test

一般情况下有包名的路径我们都是调用Context中的方法来获得,没有包名的路径,我们直接调用Environment中的方法获得。

  • 扩展卡内存
    扩展内存就是我们插入的外置SD卡
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;
}

你可能感兴趣的:(Android)