gpt4 book ai didi

Android 6(M) 权限问题(创建目录不工作)

转载 作者:可可西里 更新时间:2023-11-01 19:09:19 25 4
gpt4 key购买 nike

我有这段代码用于创建保存图片的目录:

        File storageDir = null;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myphoto");
if (!storageDir.mkdirs()) {
if (!storageDir.exists()){
Log.d("photo", "failed to create directory");
return null;
}
}
}
return storageDir;

storeDir 返回 "/storage/emulated/0/Pictures/myphoto/"在 android 6 以下和 android 6 上它返回 null .

我有权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

buildToolVersion 23
targetSdkVersion 23

如何修复?

最佳答案

正如@CommonsWare 回答的那样,在 Android M 上有运行时权限询问概念,因此在新方法中,安装应用程序时不询问权限,但在运行时尝试使用需要权限的手机特定功能时.用户稍后也可以通过 phone settings->app->yourapp->permissions 禁用权限。所以你必须在使用该权限做某事之前进行检查,并询问用户:

int REQUEST_WRITE_EXTERNAL_STORAGE=1;
////...
File storageDir = null;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
//RUNTIME PERMISSION Android M
if(PackageManager.PERMISSION_GRANTED==ActivityCompat.checkSelfPermission(context,Manifest.permission.WRITE_EXTERNAL_STORAGE)){
storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myPhoto");
}else{
requestPermission(context);
}

}
return storageDir;
////...
private static void requestPermission(final Context context){
if(ActivityCompat.shouldShowRequestPermissionRationale((Activity)context,Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// For example if the user has previously denied the permission.

new AlertDialog.Builder(context)
.setMessage(context.getResources().getString(R.string.permission_storage))
.setPositiveButton(R.string.tamam, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions((Activity) context,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_WRITE_EXTERNAL_STORAGE);
}
}).show();

}else {
// permission has not been granted yet. Request it directly.
ActivityCompat.requestPermissions((Activity)context,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_WRITE_EXTERNAL_STORAGE);
}
}

///...

@Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
switch (requestCode) {
case UtilityPhotoController.REQUEST_WRITE_EXTERNAL_STORAGE: {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context,
getResources().getString(R.string.permission_storage_success),
Toast.LENGTH_SHORT).show();

} else {
Toast.makeText(context,
getResources().getString(R.string.permission_storage_failure),
Toast.LENGTH_SHORT).show();
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
return;
}
}
}

关于Android 6(M) 权限问题(创建目录不工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32225506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com