Android 6.0 棉花糖.无法写入 SD 卡 [英] Android 6.0 Marshmallow. Cannot write to SD Card

查看:31
本文介绍了Android 6.0 棉花糖.无法写入 SD 卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用外部存储器来存储照片的应用.根据需要,在其清单中,请求以下权限

I have an app that uses external storage to store photographs. As required, in its manifest, the following permissions are requested

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

它使用以下内容来检索所需的目录

and it uses the following to retrieve the required directory

File sdDir = Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd", Locale.US);
String date = dateFormat.format(new Date());
storageDir = new File(sdDir, getResources().getString(
            R.string.storagedir)
            + "-" + date);

// Create directory, error handling
if (!storageDir.exists() && !storageDir.mkdirs()) {
 ... fails here

该应用在 Android 5.1 到 2.3 上运行良好;它已在 Google Play 上架一年多了.

The app works fine on Android 5.1 to 2.3; it has been on Google Play for over a year.

在将我的一款测试手机(Android One)升级到 6 之后,它现在在尝试创建必需目录/sdcard/Pictures/myapp-yy-mm"时返回错误.

Following an upgrade of one of my testing phones (Android One) to 6, it's now returning an error when trying to create the requisite directory, "/sdcard/Pictures/myapp-yy-mm".

sd 卡被配置为便携式存储".我已经格式化了sd卡.我已经换了.我重启了一切都无济于事.

The sd card is configured as "Portable storage". I've formatted the sd card. I've replaced it. I've rebooted. All to no avail.

此外,内置的 android 屏幕截图功能(通过电源+降低音量)由于存储空间有限,或者应用或您的组织不允许"而失败.

Also, the built-in android screenshot functionality (via Power+Lower volume) is failing "due to limited storage space, or it isn't allowed by the app or your organisation".

有什么想法吗?

推荐答案

我遇到了同样的问题.Android 中有两种类型的权限:

I faced the same problem. There are two types of permissions in Android:

  • 危险(访问联系人、写入外部存储...)
  • 正常

普通权限由Android自动批准,危险权限需要Android用户批准.

Normal permissions are automatically approved by Android while dangerous permissions need to be approved by Android users.

这是在 Android 6.0 中获取危险权限的策略

Here is the strategy to get dangerous permissions in Android 6.0

  1. 检查您是否获得了权限
  2. 如果您的应用已被授予权限,请继续并正常执行.
  3. 如果您的应用还没有权限,请请求用户批准
  4. 在 onRequestPermissionsResult 中听取用户的批准

这是我的情况:我需要写入外部存储.

Here is my case: I need to write to external storage.

首先,我检查我是否有权限:

First, I check if I have the permission:

...
private static final int REQUEST_WRITE_STORAGE = 112;
...
boolean hasPermission = (ContextCompat.checkSelfPermission(activity,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
    ActivityCompat.requestPermissions(parentActivity,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                REQUEST_WRITE_STORAGE);
}

然后检查用户的批准:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode)
    {
        case REQUEST_WRITE_STORAGE: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                //reload my activity with permission granted or use the features what required the permission
            } else
            {
                Toast.makeText(parentActivity, "The app was not allowed to write to your storage. Hence, it cannot function properly. Please consider granting it this permission", Toast.LENGTH_LONG).show();
            }
        }
    }

}

您可以在此处阅读有关新权限模型的更多信息:https://developer.android.com/training/permissions/requesting.html

You can read more about the new permission model here: https://developer.android.com/training/permissions/requesting.html

这篇关于Android 6.0 棉花糖.无法写入 SD 卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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