我正在我的 native 应用程序中创建目录。它在 Android 5.0 中运行良好,但在 Android 6.0 (Marshmallow) 中有时有效,有时无效。我正在使用以下代码创建新目录。我正在保存视频和目录中的图像文件。
public static void createApplicationFolder() {
File f = new File(Environment.getExternalStorageDirectory(), File.separator + Config.VIDEO_COMPRESSOR_APPLICATION_DIR_NAME);
f.mkdirs();
f = new File(Environment.getExternalStorageDirectory(), File.separator + Config.VIDEO_COMPRESSOR_APPLICATION_DIR_NAME + Config.VIDEO_COMPRESSOR_COMPRESSED_VIDEOS_DIR);
f.mkdirs();
f = new File(Environment.getExternalStorageDirectory(), File.separator + Config.VIDEO_COMPRESSOR_APPLICATION_DIR_NAME + Config.VIDEO_COMPRESSOR_TEMP_DIR);
f.mkdirs();
}
最佳答案
public class MyDevIDS extends AppCompatActivity {
private static final int REQUEST_RUNTIME_PERMISSION = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (CheckPermission(MyDevIDS.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// you have permission go ahead
createApplicationFolder();
} else {
// you do not have permission go request runtime permissions
RequestPermission(MyDevIDS.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_RUNTIME_PERMISSION);
}
}
@Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {
switch (permsRequestCode) {
case REQUEST_RUNTIME_PERMISSION: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// you have permission go ahead
createApplicationFolder();
} else {
// you do not have permission show toast.
}
return;
}
}
}
public void RequestPermission(Activity thisActivity, String Permission, int Code) {
if (ContextCompat.checkSelfPermission(thisActivity,
Permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Permission)) {
} else {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Permission},
Code);
}
}
}
public boolean CheckPermission(Context context, String Permission) {
if (ContextCompat.checkSelfPermission(context,
Permission) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
}
关于java - 目录创建在 Marshmallow Android 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38141523/