我使用此代码创建文件夹。
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" />
请帮帮我。谢谢你的阅读。
最佳答案
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();
}
}