原因:java.lang.IllegalStateException:无法在Android 6.0设备中创建目录 [英] Caused by: java.lang.IllegalStateException: Unable to create directory in Android 6.0 devices

查看:267
本文介绍了原因:java.lang.IllegalStateException:无法在Android 6.0设备中创建目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用DownloadManager存储从url下载的图像,并将其存储到sdcard中,并带有我自己的目录,例如"xyz".这是我的代码

I have to store the download the image from the url using DownloadManager and store it into the sdcard with my own directory like "xyz". This is my code

File img_directory = null;

img_directory = new File(Environment.getExternalStorageDirectory() + "/xyz");
if (!img_directory.exists()) {
    img_directory.mkdirs();
    DownloadManager mgr = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    Uri downloadUri = Uri.parse("my image url");
    DownloadManager.Request request = new DownloadManager.Request(downloadUri);
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
      .setAllowedOverRoaming(true)
      .setTitle("Demo")
      .setDescription("Something useful. No, really.")
      .setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
      .setDestinationInExternalPublicDir(Environment.getExternalStorageDirectory().getAbsolutePath() + "/xyz", image.jpeg);
    mgr.enqueue(request);
}

此代码将在Android 5.1.1上运行.当我在6.0中运行相同的代码时,会引发这样的错误

This code will run upto Android 5.1.1 . When I run this same code in 6.0 its raising the error like this

Caused by: java.lang.IllegalStateException: Unable to create directory: /storage/emulated/0/storage/emulated/0/xyz at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:538)

我已在清单文件中添加了READ和WRITE权限.如何解决此错误?有没有人可以帮助我?预先感谢.

I have added the READ and WRITE permissions in manifest file. How can I resolve this error? Any body can help me ? Thanks in advance.

推荐答案

Caused by: java.lang.IllegalStateException: Unable to create directory: /storage/emulated/0/storage/emulated/0/xyz at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:538)

问题似乎与 Android运行时权限 在Android 6.0中引入

Issue seems it be related with Android Runtime Permission introduced in Android 6.0

当您的应用定位为API级别23或更高级别时,默认情况下,所有权限均为false.要解决此问题,您必须先请求一个权限对话框并批准该权限,然后才能将其用于您的应用程序.

When your app targeting is API Level 23 or higher, by default all the permissions are false. To resolve this, you have to request a permission dialog and approve the permission before using that into your app.

这篇关于原因:java.lang.IllegalStateException:无法在Android 6.0设备中创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆