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

查看:1140
本文介绍了安卓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;它已经对谷歌玩了一年多了。

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

继我的测试手机(安卓一)至6之一的升级,它现在试图创建必要的目录时,返回一个错误,/ SD卡/图片/ 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屏幕截图功能(通过Power +降低音量)未能由于存储空间有限,或者不被应用程序或您的组织允许的。

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用户的批准。

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

下面是战略取得的Andr​​oid 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);
}

然后检查用户的认可:

Then check the user's approval:

@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();
            }
        }
    }

}

您可以阅读更多关于此新的权限模型:<一href="https://developer.android.com/training/permissions/requesting.html">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

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

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