Android 4.4之后WRITE_MEDIA_STORAGE 权限仅提供给系统应用,不再授予第三方App,WRITE_EXTERNAL_STORAGE 权限,仅仅用于授权用户写 primary external storage,除了与自己包名相关的文件夹之外,应用程序不允许写secondary external storage devices;
举例来说,如果应用的包名是com.travis.example,那么外部存储上的Android/data/com.travis.example/文件夹就可随意访问,其他任何地方都不允许写,并且,存储在自己包名相关的文件夹的文件,当该应用被卸载时候也会随之被清除。
分以下三种情况:
只有外部存储的设备,这种设备一般是android4.0之前的,只有一个存储,不受这个规则限制,还是可以随便读写,但如果高于4.4系统,那么就只能写自己包名相关(上述规则)的文件夹了。
只有内部存储的设备,不受这个规则限制,但是建议在自己的包名相关的文件夹写数据。
既有内部存储又有外部存储需要遵守这个规定,不可以在外部存储乱写了,需要在自己的包名相关的文件夹写数据。
Google官网原话如此:
The WRITE_EXTERNAL_STORAGE permission must only grant write access to the primary external storage on a device. Apps must not be allowed to write to secondary external storage devices, except in their package-specific directories as allowed by synthesized permissions.Restricting writes in this way ensures the system can clean up fileswhen applications are uninstalled.
先给出创建后的效果日志:
具体的Log日志如下:
08-15 14:00:53.005 8458-8458/com.flash E/Logger: [ (SdCardUtil.java:98)#main ] 路径:/storage/emulated/0
08-15 14:00:53.006 8458-8458/com.flash E/Logger: [ (SdCardUtil.java:98)#main ] 路径:/storage/uicc0
08-15 14:00:53.006 8458-8458/com.flash E/Logger: [ (SdCardUtil.java:98)#main ] 路径:/storage/sdcard1
08-15 14:00:53.010 8458-8458/com.flash E/Logger: [ (MyApplication.java:39)#main ] 文件路径:/storage/sdcard1/Android/data/com.flash/cache/
08-15 14:00:53.524 8458-8577/com.flash E/Logger: [ (MainActivity.java:234)#Thread-2256 ] /storage/sdcard1/Android/data/com.flash/cache/photos/q
08-15 14:00:53.799 8458-8577/com.flash E/Logger: [ (MainActivity.java:234)#Thread-2256 ] /storage/sdcard1/Android/data/com.flash/cache/photos/u
08-15 14:00:53.956 8458-8577/com.flash E/Logger: [ (MainActivity.java:234)#Thread-2256 ] /storage/sdcard1/Android/data/com.flash/cache/photos/w
08-15 14:00:54.118 8458-8577/com.flash E/Logger: [ (MainActivity.java:234)#Thread-2256 ] /storage/sdcard1/Android/data/com.flash/cache/videos
08-15 14:00:54.230 8458-8577/com.flash E/Logger: [ (MainActivity.java:234)#Thread-2256 ] /storage/sdcard1/Android/data/com.flash/cache/apk
08-15 14:00:54.318 8458-8577/com.flash E/Logger: [ (MainActivity.java:234)#Thread-2256 ] /storage/sdcard1/Android/data/com.flash/cache/shanghai/w
08-15 14:00:54.577 8458-8577/com.flash E/Logger: [ (MainActivity.java:234)#Thread-2256 ] /storage/sdcard1/Android/data/com.flash/cache/images
很明显,属于第三种情况,存在外部存储,也存在内部存储的情况,因此在外置SD卡中创建文件夹的时候,需要按照Google官方给出的规则来创建,即为:外部路径 + Android + data + 包名 + cache / file + 需要创建的文件夹名称;
具体代码如下:
/**
* 获取外置SD卡路径
*/
public static List<String> getExtSDCardPathList() {
List<String> paths = new ArrayList<String>();
String extFileStatus = Environment.getExternalStorageState();
File extFile = Environment.getExternalStorageDirectory();
//首先判断一下外置SD卡的状态,处于挂载状态才能获取的到
if (extFileStatus.equals(Environment.MEDIA_MOUNTED) && extFile.exists() && extFile.isDirectory() && extFile.canWrite()) {
//外置SD卡的路径
paths.add(extFile.getAbsolutePath());
}
try {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("mount");
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
int mountPathIndex = 1;
while ((line = br.readLine()) != null) {
// format of sdcard file system: vfat/fuse
if ((!line.contains("fat") && !line.contains("fuse") && !line
.contains("storage"))
|| line.contains("secure")
|| line.contains("asec")
|| line.contains("firmware")
|| line.contains("shell")
|| line.contains("obb")
|| line.contains("legacy") || line.contains("data")) {
continue;
}
String[] parts = line.split(" ");
int length = parts.length;
if (mountPathIndex >= length) {
continue;
}
String mountPath = parts[mountPathIndex];
if (!mountPath.contains("/") || mountPath.contains("data")
|| mountPath.contains("Data")) {
continue;
}
File mountRoot = new File(mountPath);
if (!mountRoot.exists() || !mountRoot.isDirectory()
|| !mountRoot.canWrite()) {
continue;
}
boolean equalsToPrimarySD = mountPath.equals(extFile
.getAbsolutePath());
if (equalsToPrimarySD) {
continue;
}
//扩展存储卡即TF卡或者SD卡路径
paths.add(mountPath);
}
} catch (IOException e) {
e.printStackTrace();
}
return paths;
} public static String getTFSDCardPath() {
List<String> list;
list = getExtSDCardPathList();
return list.get(list.size() - 1);
} /**
* 获取外置SD卡存储文件的绝对路径
* Android 4.4以后
* @param context
*/
public static String getExternalFileDir(Context context) {
StringBuilder sb = new StringBuilder();
File file = context.getExternalCacheDir(); //此句代码一定要,在内部存储空间创建对应的data目录,但不存储文件
if (file.exists()) {
sb.append(SdCardUtil.getTFSDCardPath().toString()).append("/Android/data/").append(context.getPackageName())
.append("/cache").append(File.separator).toString();
} else {
sb.append(SdCardUtil.getTFSDCardPath().toString()).append("/Android/data/").append(context.getPackageName())
.append("/cache").append(File.separator).toString();
}
return sb.toString();
}
友情提示:转载请注明出处,谢谢!