Android 基础知识之存储路径

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/c_he_n/article/details/83619632

其实很多人并不能分清Android中内存存储和外部存储和sdcard,大部分人分不清的主要原因在原Environment.getExternalStorageDirectory(),总认为这个是获取sdcard的,在api中getExternalStorageDirectory()备注是这么说的

Note: don’t be confused by the word “external” here. This directory can better be thought as media/shared storage.

大致的意思就是不要被external迷惑了,这个目录最好被理解位媒体/共享存储。出现这个原因的还有就是各大定制rom的大厂,把内置的sd卡叫做内部存储,导致这个内部存储和Google定义的内部存储就混淆了。
在区分这些目录之前,首先看看Android 是怎么区分这些存储的,清空大脑,不要想那些sdcard,内部存储,我们现在要重新梳理下这些东西,并对应于environment中那个函数。
Android
Android 中对存储分为两类,一类就是内部存储,另一类是外部存储,

  • 内部存储
    内部存储存放app私有数据,不与其他app共享数据,但是空间小,而且在删除app的时候对应的数据也会删除,清理缓存的时候也会丢失部分数据。
方法 路径
Environment.getDataDirectory().getAbsolutePath() /data
Environment.getDownloadCacheDirectory().getAbsolutePath() /data/cache
Environment.getRootDirectory().getAbsolutePath() /system
  • 外置存储-SD卡
    外置存储空间大,但是其他app都可访问,导致数据的泄露等问题
方法 路径
Environment.getExternalStorageDirectory().getAbsolutePath() /storage/emulated/0
Environment.getExternalStoragePublicDirectory(DIRECTORY_MUSIC).getAbsolutePath() /storage/emulated/0/Music
Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOAD).getAbsolutePath() /storage/emulated/0/Download

打开Android Studio 中device explorer 可以清晰找到对应的目录,内部存储

  • 外置存储-外置卡
    外置卡,也就是我们所说的真正的sd卡,因为Android的开源,导致外置卡在系统的存放路径各大厂商不一样,所以处理起来比较麻烦,好在Google给了解决方案,可以通过storagemanager来实现。
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;
}

系统中各个函数对应的目录

 Log.i("storage:", "内部存储:");
 Log.i("storage:",  Environment.getDataDirectory().getAbsolutePath());
 Log.i("storage:",  Environment.getDownloadCacheDirectory().getAbsolutePath());
 Log.i("storage:",  Environment.getRootDirectory().getAbsolutePath());
 Log.i("storage:", "内嵌的Sdcard存储:");
 Log.i("storage:", Environment.getExternalStorageDirectory().getAbsolutePath());
 Log.i("storage:", + Environment.getExternalStoragePublicDirectory(DIRECTORY_MUSIC).getAbsolutePath());
 Log.i("storage:", "外置的sdcard");
 Log.i("storage:", SDCardHelper.getUsbPath(this));

内部存储:
/data
/data/cache
/system
内嵌的Sdcard存储:
/storage/emulated/0
/storage/emulated/0/Music
外置的sdcard
/storage/sd89-8768

打开Android Studio device explorer 会看到有两个sdcard目录,而storage/sdcard0才是真正的目录,也就是我们常说的“内部存储”,sdcard是它的快捷方式。在我这个目录你还发现有sdcard1,他是外置sdcard的目录,就是我们常说的sdcard目录,还有个usbotg目录,这个目录是usb的目录。
文件目录
要是你在目录找找,你会找到如下的目录:
在这里插入图片描述
mnt 全称为mount,挂载的目录,为什么是sdcard0,1……,请看参考博客,写的不错。

参考

Android存储路径你了解多少?
Android内、外存储 易混淆点剖析

猜你喜欢

转载自blog.csdn.net/c_he_n/article/details/83619632
今日推荐