在Sdcard中创建文件夹失败(三星Galaxy)

时间:2016-12-15 11:16:50

标签: android

无法在三星Galaxy中创建文件夹。我试图通过调用此方法创建一个文件夹:     稍后获取存储路径我正在尝试将文件夹名称添加到路径并尝试创建

  Its even showing the path...but no such file was created.

public static String createFolder(String folderName) {
    String extFolName =   Environment.getExternalStorageDirectory().toString();

    f = new File(extFolName + "/" + folderName + "/");
    if (f.exists()) {
        path = f.getAbsolutePath();
        return path;
    } else {
        if (f.mkdir()) {
            path = f.getAbsolutePath();
            return path;
        }
 else
 {
 //always going to else don't know y //
    }
    return path;
}

当我尝试检查在SD卡中创建的文件夹时,没有文件创建...

2 个答案:

答案 0 :(得分:1)

public static String createFolder(String folderName) {
String extFolName =   Environment.getExternalStorageDirectory().toString();

f = new File(extFolName + "/" + folderName + "/");
if (f.exists()) {
    path = f.getAbsolutePath();
    return path;
} else {
    if (f.mkdirs()) {
        path = f.getAbsolutePath();
        return path;
    }
 else
 {
 //now it will not go to the else part //
 }
return path;
}

答案 1 :(得分:0)

您是否关注<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 。此外,如果您使用的是Android 6(API级别23)或更高版本,则需要请求该用户接受该权限。

{{1}}