之前在Activity里的onCreate方法里写了个创建文件夹得方法,但是只在安卓4.4成功创建了,但是在安卓5.0及以上版本都不能成功,有没有大佬帮忙指导下的。
一样可以创建,前提是你必须有权限,执行 adb shell ls -l 查看权限
http://blog.csdn.net/yukun314/article/details/52083534
动态申请权限
Android4.4开始,如果设备有内部机身存储,那么SD就成为二级外部存储,导致不能写入文件,因为默认只能写入以及存储。在Android开发者网站的“外部存储技术信息”文档中的描述:"WRITE_EXTERNAL_STORAGE只为设备上的主要外部存储授予写权限,应用程序无法将数据写入二级外部存储设备,除非指定了应用程序允许访问的特定的目录。“ Google表示,这样做的目的是,通过这种方式进行限制,系统可以在应用程序被卸载后清除遗留文件。
这目前只影响双存储设备,果你同时使用了机身存储和SD卡,那么应用程序将无法在SD卡中创建、修改、删除数据。
会写入到如下位置:
解决办法:1.对Android手机用户来讲,获得系统的ROOT权限是一个解决方法。2.对Android开发者来讲,可在应用中嵌入一段代码,其它方式写入失败,则将数据写入二级存储设备(这段代码作用是在Android 4.4+设备上):
最后记得增加权限
需要动态增加创建文件夹的权限
你要先进程序列表,再点菜单键-编辑-新建文件夹。建好后再拖到桌面就好了~~这时你会发现程序列表那个文件夹还在,你可以重命名再用或删除都可以。
/**
* 获取外置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();
}
/**
* 获取外置SD卡路径
*/
public static List getExtSDCardPathList() {
List paths = new ArrayList();
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;
}
//扩展存储卡sdcard卡路径
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();
}
申请动态权限之后进行创建
可以使用zarchiver工具来创建,网上搜得到
动态申请权限,在创建文件夹和文件之前先判断一下Android版本,高于5.0就提示需要权限,用户同意后就执行创建,反之亦然。
这个必须得申请文件权限