0

I've been looking around google and stack overflow for answers to this question. Why can't I create a folder in the DCIM directory with the correct permissions in my android manifest file... Here's the code, hopefully someone can help me.

    final File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "mycustomdirectory");

    if (!f.exists()) {
        Log.d("folder", "Folder doesn't exist, creating it...");
        boolean rv = f.mkdir();
        Log.d("folder", "Folder creation " + ( rv ? "success" : "failed"));
    } else {
        Log.d("folder", "Folder already exists.");
    }

Found this code on stackoverflow....

David Kachlon
  • 201
  • 3
  • 14

1 Answers1

0

after android 6.0, you need to check or request permissions dynamically. use the following code to check if need permission

private boolean needPermission() {
    return ContextCompat.checkSelfPermission(
            ApplicationUtils.getAppContext(), WRITE_EXTERNAL_STORAGE_PERM) !=
            PackageManager.PERMISSION_GRANTED;
}

and use the following code to request permission

public void requestPermission() {
    if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        showDialog(R.string.title_permission, R.string.rational_permission_writing_external_storage,
                android.R.string.ok, android.R.string.cancel, false,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i("TAG", "should try to request the permission");
                        requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE_PERM},
                                REQUEST_WRITE_EXTERNAL_STORAGE);
                    }
                }, null);
    } else {
        Log.i("TAG", "should try to request the permission");
        requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE_PERM},
                REQUEST_WRITE_EXTERNAL_STORAGE);
    }
}
D.Jiang
  • 199
  • 5