【问题标题】:why get java.lang.IllegalStateException: Unable to create directory error?为什么会出现 java.lang.IllegalStateException: Unable to create directory 错误?
【发布时间】:2015-01-04 07:56:54
【问题描述】:

我使用 DownloadManager 类在 android 中下载文件

这是我的下载代码:

Uri downloadUri = Uri.parse(urlString);
DownloadManager.Request request = new
DownloadManager.Request(downloadUri);
            request.setDescription(des).setTitle(titleAudio).setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED).
                        setDestinationInExternalPublicDir(
                                "/my file name/" , titleAudio + String.valueOf(colForUrl) + pasvand);

                long id = downloadManager.enqueue(request);

                SharedPreferences.Editor prefEdit = preferenceManager.edit();
                prefEdit.putLong(strPref_Download_ID, id);
                prefEdit.commit();

但是当我在某些设备(Samsung Galaxy S5)上运行应用程序时,我收到了这个错误:

java.lang.IllegalStateException: Unable to create directory: /storage/emulated/0/my file name

这是在 setDestinationInExternalPublicDir(..)

中引起的

但在 Nexus 7 中,一切都是正确的,我没有任何问题!!
那么,我哪里错了?!

【问题讨论】:

    标签: android android-file android-download-manager android-internet


    【解决方案1】:

    根据docs

    setDestinationInExternalPublicDir(String dirType, String subPath)

    • dirType - 传递给 getExternalStoragePublicDirectory(String) 的目录类型
    • subPath - 外部目录中的路径,包括目标文件名

    dirType 应该是 DIRECTORY_MUSIC、DIRECTORY_PODCASTS、 DIRECTORY_RINGTONES、DIRECTORY_ALARMS、DIRECTORY_NOTIFICATIONS、 DIRECTORY_PICTURES、DIRECTORY_MOVIES、DIRECTORY_DOWNLOADS 或 目录_DCIM。不能为空。

    因此,例如,尝试:

    setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, titleAudio + String.valueOf(colForUrl) + pasvand);

    如果它不是您想要的文件夹,则从那里开始。

    【讨论】:

    • 但是在nexus 7中我运行应用程序并且它的工作!创建我的目录没有任何错误!我想这是因为公共存储! Galaxy S5和S4公共存储是sd卡,但nexus7没有sd卡!你怎么看?!
    • 是的,也许这就是问题的一部分。您是否在清单上设置了 WRITE_EXTERNAL_STORAGE 权限?
    • 是的,那么我如何在目录的根目录中创建文件?!
    【解决方案2】:

    Android API 级别 23 具有权限模型的新概念。该概念建议在运行时检查并请求用户许可。

    系统权限分为两类,普通权限和 危险:

    1- 普通权限不会直接危及用户的隐私。如果你的 应用程序在其清单中列出了正常权限,系统授予 自动获得许可。

    2- 危险的权限可以给应用程序 访问用户的机密数据。如果您的应用列出了一个正常的 清单中的权限,系统授予权限 自动地。如果您列出危险权限,用户必须 明确批准您的应用。

    WRITE_EXTERNAL_STORAGE属于危险权限,因此您必须签入运行时才能获得创建目录的权限。

    第一步是在 Manifest.xml 中添加许可请求,方法是添加以下内容:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    上述权限将不允许应用程序从Android 6.0写入外部存储,因此需要在运行时向用户询问权限。

    检查权限:

    if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.READ_CONTACTS)
            != PackageManager.PERMISSION_GRANTED) {
    
        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                Manifest.permission.READ_CONTACTS)) {
    
            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.
    
        } else {
    
            // No explanation needed, we can request the permission.
    
            ActivityCompat.requestPermissions(thisActivity,
                    new String[]{Manifest.permission.READ_CONTACTS},
                    MY_PERMISSIONS_REQUEST_READ_CONTACTS);
    
            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }
    

    权限请求方法:

    @Override
    public void onRequestPermissionsResult(int requestCode,
            String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.
    
                } else {
    
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }
    
            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
    

    访问Requesting Permissions at Run Time了解更多信息和所需代码。

    【讨论】:

      猜你喜欢
      • 2020-02-03
      • 2018-04-25
      • 2020-02-09
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      • 2022-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多