Android:无法用带有棉花糖的三星设备写入sdcard

时间:2017-03-16 12:15:11

标签: android file android-6.0-marshmallow sd-card

大家好,

我正在尝试使用marshmallow在samsung设备的sdcard中创建一个文件夹。但是api,dir.canWrite()失败了,这意味着我无法写入那条路径。我在moto x中尝试过它,它的工作原理。

有人请帮助我

更新

我拥有Manifest中的所有权限以及运行时权限的代码。

清单文件:

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

运行时权限代码:

private final int WRITE_STORAGE_CODE  = 10; 
@TargetApi(23)
public  boolean isStoragePermissionGrantedForWriteStorage() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG,"Permission is granted");
            return true;
        } else {

            Log.v(TAG,"Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_STORAGE_CODE);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v(TAG,"Permission is granted");
        return true;
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case WRITE_STORAGE_CODE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                File dir = new File("/storage/6A6B-1CEF/"); // This is the memory card location.

                if(dir.canWrite())
                {
                    Log.d("Activity", can write here!!);
                }else{
                    Log.d("Activity", oops!!!!, can't write here!!);                    }
            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                Log.d("Activity", permission denied);
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

因此,每当我在三星设备中调用此代码时,我打印的内容都无法在该位置写入。但是相同的代码允许我在Moto x设备中编写。

注意:

我读过Marshmallow可以选择将存储卡作为“内部存储”或“便携式存储”。我已经在手机(三星和Moto)中选择了便携式内存,但此代码仅在三星中失败。

1 个答案:

答案 0 :(得分:0)

您在清单文件中添加了权限吗?

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