本文整理了一些安卓中常用的获取文件夹的api, 并得到它们的实际存储地址, 方便开发者了解在做文件存储时,文件会具体存放的位置所在。
代码运行环境为华为P9 Android 8.0, 项目版本配置如下:
compileSdkVersion 28
minSdkVersion 22
targetSdkVersion 28
不同版本手机,打印出来的地址可能会有出入, 建议可以复制如下代码进行实际测试:
kotlin版本
private fun printFileDir() {
var paths: StringBuilder = java.lang.StringBuilder()
paths.append("\n------------------------------------------------------------------------------- \n")
paths.append("\n获取目录api对应的存储地址如下: \n")
paths.append("\nEnvironment.getDataDirectory() :\t" + Environment.getDataDirectory())
paths.append("\nEnvironment.getDownloadCacheDirectory() :\t" + Environment.getDownloadCacheDirectory())
paths.append("\nEnvironment.getExternalStorageDirectory() :\t" + Environment.getExternalStorageDirectory())
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_ALARMS
)
)
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM
)
)
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS
)
)
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS
)
)
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES
)
)
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MUSIC
)
)
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_NOTIFICATIONS
)
)
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES
)
)
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PODCASTS
)
)
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_RINGTONES
)
)
paths.append("\nEnvironment.getExternalStorageState() :\t" + Environment.getExternalStorageState())
paths.append("\nEnvironment.getRootDirectory() :\t" + Environment.getRootDirectory())
paths.append("\nContextCompat.getExternalCacheDirs(context!!) :\t" + ContextCompat.getExternalCacheDirs(context!!).get(0).path)
paths.append(
"\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_ALARMS) :\t" + ContextCompat.getExternalFilesDirs(
context!!,
Environment.DIRECTORY_ALARMS
).get(0).path
)
paths.append(
"\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_DCIM) :\t" + ContextCompat.getExternalFilesDirs(
context!!,
Environment.DIRECTORY_DCIM
).get(0).path
)
paths.append(
"\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_DOCUMENTS) :\t" + ContextCompat.getExternalFilesDirs(
context!!,
Environment.DIRECTORY_DOCUMENTS
).get(0).path
)
paths.append(
"\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_DOWNLOADS) :\t" + ContextCompat.getExternalFilesDirs(
context!!,
Environment.DIRECTORY_DOWNLOADS
).get(0).path
)
paths.append(
"\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_MOVIES) :\t" + ContextCompat.getExternalFilesDirs(
context!!,
Environment.DIRECTORY_MOVIES
).get(0).path
)
paths.append(
"\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_MUSIC) :\t" + ContextCompat.getExternalFilesDirs(
context!!,
Environment.DIRECTORY_MUSIC
).get(0).path
)
paths.append(
"\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_NOTIFICATIONS) :\t" + ContextCompat.getExternalFilesDirs(
context!!,
Environment.DIRECTORY_NOTIFICATIONS
).get(0).path
)
paths.append(
"\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_PICTURES) :\t" + ContextCompat.getExternalFilesDirs(
context!!,
Environment.DIRECTORY_PICTURES
).get(0).path
)
paths.append(
"\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_PODCASTS) :\t" + ContextCompat.getExternalFilesDirs(
context!!,
Environment.DIRECTORY_PODCASTS
).get(0).path
)
paths.append(
"\nContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_RINGTONES) :\t" + ContextCompat.getExternalFilesDirs(
context!!,
Environment.DIRECTORY_RINGTONES
).get(0).path
)
paths.append("\n------------------------------------------------------------------------------- \n")
paths.append("\n以上为常用目录打印结果\n")
Log.d("file_operator", paths.toString())
}
注意,
ContextCompat.getExternalFilesDirs()
是为Android 4.3及以下版本为获取外置存储目录的兼容方法,返回的是一个文件数组列表。如果你的应用兼容到4.3及以下,在选择外置存储时,请默认选择第一个文件路径,只有当它控件满了或者不可用时,在选择其它可用的外置存储。
Java版本
private void printFileDir(Context context) {
StringBuilder paths = new StringBuilder();
paths.append("\n------------------------------------------------------------------------------- \n");
paths.append("\n获取目录api对应的存储地址如下: \n");
paths.append("\nEnvironment.getDataDirectory() :\t" + Environment.getDataDirectory());
paths.append("\nEnvironment.getDownloadCacheDirectory() :\t" + Environment.getDownloadCacheDirectory());
paths.append("\nEnvironment.getExternalStorageDirectory() :\t" + Environment.getExternalStorageDirectory());
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_ALARMS
)
);
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM
)
);
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS
)
);
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS
)
);
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES
)
);
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MUSIC
)
);
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_NOTIFICATIONS
)
);
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES
)
);
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PODCASTS
)
);
paths.append(
"\nEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES) :\t" + Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_RINGTONES
)
);
paths.append("\nEnvironment.getExternalStorageState() :\t" + Environment.getExternalStorageState());
paths.append("\nEnvironment.getRootDirectory() :\t" + Environment.getRootDirectory());
paths.append("\nContextCompat.getExternalCacheDirs(context) :\t" + ContextCompat.getExternalCacheDirs(context));
paths.append(
"\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_ALARMS)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
context,
Environment.DIRECTORY_ALARMS
)[0].getPath()
);
paths.append(
"\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_DCIM)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
context,
Environment.DIRECTORY_DCIM
)[0].getPath()
);
paths.append(
"\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_DOCUMENTS)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
context,
Environment.DIRECTORY_DOCUMENTS
)[0].getPath()
);
paths.append(
"\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_DOWNLOADS)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
context,
Environment.DIRECTORY_DOWNLOADS
)[0].getPath()
);
paths.append(
"\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_MOVIES)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
context,
Environment.DIRECTORY_MOVIES
)[0].getPath()
);
paths.append(
"\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_MUSIC)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
context,
Environment.DIRECTORY_MUSIC
)[0].getPath()
);
paths.append(
"\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_NOTIFICATIONS)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
context,
Environment.DIRECTORY_NOTIFICATIONS
)[0].getPath()
);
paths.append(
"\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_PICTURES)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
context,
Environment.DIRECTORY_PICTURES
)[0].getPath()
);
paths.append(
"\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_PODCASTS)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
context,
Environment.DIRECTORY_PODCASTS
)[0].getPath()
);
paths.append(
"\nContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_RINGTONES)[0].getPath() :\t" + ContextCompat.getExternalFilesDirs(
context,
Environment.DIRECTORY_RINGTONES
)[0].getPath()
);
paths.append("\n------------------------------------------------------------------------------- \n");
paths.append("\n以上为常用目录打印结果\n");
Log.d("file_operator", paths.toString());
}
打印的结果如下:
-------------------------------------------------------------------------------
获取目录api对应的存储地址如下:
Environment.getDataDirectory() : /data
Environment.getDownloadCacheDirectory() : /data/cache
Environment.getExternalStorageDirectory() : /storage/emulated/0
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS) : /storage/emulated/0/Alarms
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) : /storage/emulated/0/DCIM
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) : /storage/emulated/0/Documents
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) : /storage/emulated/0/Download
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) : /storage/emulated/0/Movies
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) : /storage/emulated/0/Music
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS) : /storage/emulated/0/Notifications
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) : /storage/emulated/0/Pictures
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS) : /storage/emulated/0/Podcasts
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES) : /storage/emulated/0/Ringtones
Environment.getExternalStorageState() : mounted
Environment.getRootDirectory() : /system
ContextCompat.getExternalCacheDirs(context!!) : /storage/emulated/0/Android/data/com.finance.theinvestorroad/cache
ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_ALARMS) : /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/Alarms
ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_DCIM) : /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/DCIM
ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_DOCUMENTS) : /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/Documents
ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_DOWNLOADS) : /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/Download
ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_MOVIES) : /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/Movies
ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_MUSIC) : /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/Music
ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_NOTIFICATIONS) : /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/Notifications
ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_PICTURES) : /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/Pictures
ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_PODCASTS) : /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/Podcasts
ContextCompat.getExternalFilesDirs(context!!, Environment.DIRECTORY_RINGTONES) : /storage/emulated/0/Android/data/com.finance.theinvestorroad/files/Ringtones
-------------------------------------------------------------------------------
以上为常用目录打印结果
网友评论