0
您好所有,Android的:不能在SD卡写入三星设备与棉花糖
我想在三星设备的SD卡与棉花糖创建一个文件夹。但是,api,dir.canWrite()失败了,这意味着我不能写入该路径。同样我在moto x中尝试过它的工作。
有人请帮助我
更新:
我在清单的所有权限,也为运行许可的代码。
清单文件:
<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设备中编写代码。
注:
我已阅读,棉花糖必须有存储卡,无论是“内部存储”或“移动存储”选项。我选择了两款手机(三星和摩托罗拉)的便携式存储器,但该代码仅在三星中出现故障。
是否增加了运行时权限? – rafsanahmad007
在Marshmallow中,你需要在做任何文件操作之前先要求权限,否则会给出SecurityException。 –
'但是API,dir.canWrite()失败'。不要这样想。如果无法写入,该函数将返回false。 – greenapps