My device is Android 6.0.1 operating system, but cannot write file to external SD card, from the log, there is no authority, but I have the right to apply for success, and that the permissions in the AndroidManifest.xml file;
I use the "adb shell dumpsys package packagename" command, read and write permissions are returned to the true; Then I looked at the Settings-> Apps->My Application - > Permissions, read and write permissions have been granted;
I must use an external SD card
Has anyone encountered this problem, I think this is very strange
private void requestPermiss() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission_group.STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
Toast.makeText(this, "We need to read file from sdcard", Toast.LENGTH_SHORT).show();
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
}, 0);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 0){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED){
writeFile();
}
}
}
private void writeFile(){
new Thread(new Runnable() {
@Override
public void run() {
String sdDir = getPath_For23(MainActivity.this, 0);
File file = new File(sdDir + "/test.txt");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
private static String getPath_For23(Context context, int flag) {
try {
StorageManager sManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
Class class_StorageManager = StorageManager.class;
Method method_getVolumes = class_StorageManager.getMethod("getVolumes");
Class<?> class_VolumeInfo = Class.forName("android.os.storage.VolumeInfo");
Method method_getPath = class_VolumeInfo.getMethod("getPath");
Method method_getDisk = class_VolumeInfo.getMethod("getDisk");
Class<?> class_DiskInfo = Class.forName("android.os.storage.DiskInfo");
Method method_isSd = class_DiskInfo.getMethod("isSd");
Method method_isUsb = class_DiskInfo.getMethod("isUsb");
List<Object> volumes = (List<Object>) method_getVolumes.invoke(sManager);
for (Object volumeInfo : volumes) {
File file = (File) method_getPath.invoke(volumeInfo);
if (file != null) {
Object diskInfo = method_getDisk.invoke(volumeInfo);
if (diskInfo != null) {
boolean isSd = (boolean) method_isSd.invoke(diskInfo);
boolean isUsb = (boolean) method_isUsb.invoke(diskInfo);
switch (flag){
case 0:
if (isSd) {
return file.getAbsolutePath();
}
break;
case 1:
if (isUsb) {
return file.getAbsolutePath();
}
break;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Error log:
W/System.err: java.io.IOException: open failed: EACCES (Permission denied)
W/System.err: at java.io.File.createNewFile(File.java:939)
W/System.err: at com.example.user.test60.MainActivity$1.run(MainActivity.java:70)
W/System.err: at java.lang.Thread.run(Thread.java:818)
W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
W/System.err: at libcore.io.Posix.open(Native Method)
W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
W/System.err: at java.io.File.createNewFile(File.java:932)
W/System.err: ... 2 more