无法在Android 7.0中创建文件夹

时间:2017-07-06 09:11:29

标签: java android permissions directory android-permissions

我使用此代码创建文件夹。

 File folder2 = new File(Environment.getExternalStorageDirectory() + "/Android");
        if (!folder2.exists()) {
            folder2.mkdir();
        }


 File folder = new File(Environment.getExternalStorageDirectory() + "/Android/MyFolder");
        boolean success = true;
        if (!folder.exists()) {
            success = folder.mkdir();
        }

它正在与其他设备一起使用,但不适用于android v7.0。

我改变并尝试使用.getAbsolutePath()和.getPath(),但这些也改变了其他设备上的成功,7.0还没有。

并且清单上有足够的权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

请帮帮我。谢谢你的阅读。

2 个答案:

答案 0 :(得分:2)

public static final int MULTIPLE_PERMISSIONS = 10; // code you want.

 String[] permissions= new String[]{
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
        };


if (checkPermissions())
         //  permissions  granted.    
          createFolder();
}

private  boolean checkPermissions() {
        int result;
        List<String> listPermissionsNeeded = new ArrayList<>();
        for (String p:permissions) {
            result = ContextCompat.checkSelfPermission(getActivity(),p);
            if (result != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(p);
            }
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),MULTIPLE_PERMISSIONS );
            return false;
        }
        return true;
    }


@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MULTIPLE_PERMISSIONS:{
            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                // permissions granted.
                createFolder();

            } else {
                String permissions = "";
                for (String per : permissionsList) {
                    permissions += "\n" + per;
                }
                // permissions list of don't granted permission
            }
            return;
        }
    }
}

private void createFolder()
{
File folder2 = new File(Environment.getExternalStorageDirectory() + "/Android");
        if (!folder2.exists()) {
            folder2.mkdir();
        }


 File folder = new File(Environment.getExternalStorageDirectory() + "/Android/MyFolder");
        boolean success = true;
        if (!folder.exists()) {
            success = folder.mkdir();
        }
}

答案 1 :(得分:0)

如果您在Android 6.0或更高版本上运行您的应用。您需要在活动文件中run time permission添加此请求权限代码

gm(image)
.quality(100)
.trim()
.setFormat("jpg")
.interlace("Line")
.resize(650, null)
.noProfile()