无法在MarshMallow及以上设备上的android中创建目录(Failed to create directory in Android (on MarshMallow and above) d

编程入门 行业动态 更新时间:2024-10-28 08:19:45
无法在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; } }

更多推荐

本文发布于:2023-08-06 11:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1448462.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:目录   设备   android   MarshMallow   Failed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!