private void requestStoragePermission if BuildVERSIONSDK_INT = BuildVERSION_CODESM if ContextCompatcheckSelfPermissionthis ManifestpermissionWRITE_EXTERNAL_STORAGE != PackageManage
private void requestStoragePermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { new MaterialDialog.Builder(this) .title("我们需要权限") .content("我们要用这个权限来下载和管理文件(必要权限)") .positiveText("同意") .negativeText("拒绝") .onPositive(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE); } }) .onNegative(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { dialog.dismiss(); } }) .show(); } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE); } } } }
原文地址: https://www.cveoy.top/t/topic/jaEH 著作权归作者所有。请勿转载和采集!