Cannot create folder on Android device external storage

Asked 2 years ago, Updated 2 years ago, 91 views

1 String folder=Environment.getExternalStorageDirectory().toString()+"/data";
2 File dir = new File (folder);
3 // Create without folder
4 if(!dir.exists()){
5 boolean result=dir.mkdirs();
6 if(!result){
7 return false;
8  }
9 }

If you execute the above code,
In line 1, folder="/storage/emulated/0/data"
In line 5, result=false
and the folder creation fails.
Could you tell me how to avoid it?

The test device is Nexus 6 (Android 6.0.1).

We tried the following:

1) Android 5.x and lower devices will be successfully processed and folders will be created.
2) If you run canWrite() on the parent folder emulated or 0 the return value is false.
3) Running setWritable(true, false) on the parent folder emulated or 0 will not change the result of 2) .
4) When I try to add the folder "xxx" in File Explorer in Android Device Monitor,

New Folder Error
Reason: mkdir: '/storage/emulated/xxx': Permission denied

appears.
Permission for /storage/emulated is "drwx--x--x".

Thank you for your cooperation.

android java

2022-09-30 21:15

1 Answers

REQUEST_CODE=200;    
if(ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
  activity.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
}

Why don't you give a permission like this?


2022-09-30 21:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.