无法在MarshMallow及以上设备上的android中创建目录(Failed to create directory in Android (on MarshMallow and above) devices)
我正在尝试使用以下代码在我的SD卡上创建一个文件夹,但它失败了,之前有很多类似的问题,但是没有一个对我有帮助,这是我用onCreate()编写的代码,
File test = new File(Environment.getExternalStorageDirectory(),"my_directory"); if(!test.exists()) { try { if (test.mkdir()) { Log.d("xxx", "directory created"); } else { Log.d("xxx", "directory creation failed"); } } catch (Exception e) { e.printStackTrace(); } } else { Log.d("xxx","directory already present"); }当我运行上面的代码时没有给出任何异常,它只打印directory creation failed日志。
我也给了以下许可,
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
注意 :我使用的是小米redmi note 3,而android版本是6.0.1,如果这很重要的话。
I am trying to create a folder on my sdcard using the following code but it fails. This is my code written in onCreate():
File test = new File(Environment.getExternalStorageDirectory(),"my_directory"); if(!test.exists()) { try { if (test.mkdir()) { Log.d("xxx", "directory created"); } else { Log.d("xxx", "directory creation failed"); } } catch (Exception e) { e.printStackTrace(); } } else { Log.d("xxx","directory already present"); }When I run the above code does not give any exception it just prints the directory creation failed log.
I have also given the following permission,
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I am using Xiaomi Redmi note 3 and Android version is 6.0.1.
最满意答案
试试这个代码
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (checkPermission()) { //do your work } else { requestPermission(); } } } protected boolean checkPermission() { int result = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE); if (result == PackageManager.PERMISSION_GRANTED) { return true; } else { return false; } } protected void requestPermission() { if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) { Toast.makeText(this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show(); } else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100); } } } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case 100: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { //do your work } else { Log.e("value", "Permission Denied, You cannot use local drive ."); } break; } }Try this code
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (checkPermission()) { //do your work } else { requestPermission(); } } } protected boolean checkPermission() { int result = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE); if (result == PackageManager.PERMISSION_GRANTED) { return true; } else { return false; } } protected void requestPermission() { if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) { Toast.makeText(this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show(); } else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100); } } } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case 100: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { //do your work } else { Log.e("value", "Permission Denied, You cannot use local drive ."); } break; } }更多推荐
发布评论