我正在尝试从Gallery中获取图像并将其设置为imageview,但在Android 6中存在一些权限问题.以下是要求许可的方法.我应该要求读外部存储还是写外部存储?
这是我到目前为止所做的:
private static final int READ_CONTACTS_PERMISSIONS_REQUEST = 1; public void getPermissionToReadExternalStorage() { if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { if (shouldShowRequestPermissionRationale( Manifest.permission.READ_EXTERNAL_STORAGE)) { requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_CONTACTS_PERMISSIONS_REQUEST); }}} @Override public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults){ // Make sure it's our original READ_CONTACTS request if (requestCode == READ_CONTACTS_PERMISSIONS_REQUEST) { if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(getActivity(), "Read Contacts permission granted", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getActivity(), "Read Contacts permission denied", Toast.LENGTH_SHORT).show(); } } else { super.onRequestPermissionsResult(requestCode, permissions, grantResults); } }
现在我的点击监听器从图库中选择数据:
pro.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setType("image/*"); if (android.os.Build.VERSION.SDK_INT >= 23) { getPermissionToReadExternalStorage(); if (getPermissionToReadExternalStorage () this???? <==) startActivityForResult(Intent.createChooser(intent, "Select Picture"), 0); } else { startActivityForResult(Intent.createChooser(intent, "Select Picture"), 0); }} }); return v; }
现在我想获得方法的结果,getPermissionToReadExternalStorage()
这样我就可以开始为Android 6挑选图库的活动了.如何获得void类的结果?另一件事是我必须为我的应用程序要求的每个权限编写方法吗?
所以我完全重写了代码以请求权限.现在它支持请求多个权限并运行具有适当结果的代码.它也适用于preMarshmallow设备,因此在这种情况下您不必检查和复制代码.
首先,使用此代码创建一个Activity类(您可以扩展您需要的任何活动之王,例如AppCompatActivity):
public abstract class PermissionActivity extends AppCompatActivity { private final ArrayListpermissionListeners = new ArrayList<>(); @SuppressWarnings("unused") public void requestPermissions(int requestCode, String[] requestPermissions, PermissionListener permissionListener) { requestPermissions(requestCode, requestPermissions, null, permissionListener); } @SuppressWarnings("unused") public void requestPermissions(final int requestCode, String[] requestPermissions, String message, final PermissionListener permissionListener) { final int[] grantResults = new int[requestPermissions.length]; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { ArrayList list_notGranted = new ArrayList<>(); for (String requestPermission : requestPermissions) if (ContextCompat.checkSelfPermission(this, requestPermission) != PackageManager.PERMISSION_GRANTED) list_notGranted.add(requestPermission); if (list_notGranted.size() > 0) { permissionListeners.add(permissionListener); requestPermissions = list_notGranted.toArray(new String[list_notGranted.size()]); if (message != null) { boolean shouldShowRequestPermissionRationale = false; for (String permission : requestPermissions) if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) { shouldShowRequestPermissionRationale = true; break; } if (shouldShowRequestPermissionRationale) { final String[] f_requestPermissions = requestPermissions; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(message); DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() { @TargetApi(Build.VERSION_CODES.M) @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case DialogInterface.BUTTON_POSITIVE: PermissionActivity.super.requestPermissions(f_requestPermissions, requestCode); break; default: for (int i = 0; i < grantResults.length; i++) grantResults[i] = PackageManager.PERMISSION_DENIED; if (permissionListener != null) permissionListener.onResult(requestCode, f_requestPermissions, grantResults); break; } } }; builder.setPositiveButton("OK", onClickListener); builder.setNegativeButton("Cancel", onClickListener); builder.show(); } else { super.requestPermissions(requestPermissions, requestCode); } } else { super.requestPermissions(requestPermissions, requestCode); } } else { for (int i = 0; i < grantResults.length; i++) grantResults[i] = PackageManager.PERMISSION_GRANTED; if (permissionListener != null) permissionListener.onResult(requestCode, requestPermissions, grantResults); } } else { if (permissionListener != null) { for (int i = 0; i < grantResults.length; i++) grantResults[i] = PackageManager.PERMISSION_GRANTED; permissionListener.onResult(requestCode, requestPermissions, grantResults); } } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { for (Iterator it = permissionListeners.iterator(); it.hasNext(); ) { PermissionListener permissionListener = it.next(); if (permissionListener.onResult(requestCode, permissions, grantResults)) { it.remove(); } } } public interface PermissionListener { boolean onResult(int requestCode, String[] requestPermissions, int[] grantResults); } }
如果要从Fragments请求权限,请添加以下类:
public class PermissionFragment extends Fragment { @SuppressWarnings("unused") public void requestPermissions(int requestCode, String[] requestPermissions, PermissionActivity.PermissionListener permissionListener) { requestPermissions(requestCode, requestPermissions, null, permissionListener); } @SuppressWarnings("unused") public void requestPermissions(final int requestCode, String[] requestPermissions, String message, PermissionActivity.PermissionListener permissionListener) { ((PermissionActivity) getActivity()).requestPermissions(requestCode, requestPermissions, message, permissionListener); } }
您的活动和碎片应该扩展这些类而不是标准类.
现在,您已准备好通过调用方法来请求权限:
requestPermissions(int requestCode, String[] requestPermissions, PermissionListener permissionListener)
如果应用程序需要权限才能使用,则应调用此方法并指定说明为何需要权限的消息.
requestPermissions(int requestCode, String[] requestPermissions, String message, PermissionListener permissionListener)
不要破坏默认方法,就是这样
// DON'T USE THIS ONE! requestPermissions(String[] requestPermissions, int requestCode) // DON'T USE THIS ONE!
以下是请求联系人的示例:
private void requestAndLoadContacts() { String[] permissions = new String[]{Manifest.permission.READ_CONTACTS}; requestPermissions(REQUEST_PERMISSIONS_CONTACTS, permissions, "Read contacts permission is required for the app to work!", new PermissionListener() { @Override public boolean onResult(int requestCode, String[] requestPermissions, int[] grantResults) { // Check if the requestCode is ours if (requestCode == REQUEST_PERMISSIONS_CONTACTS) { // Check if the permission is correct and is granted if (requestPermissions[0].equals(Manifest.permission.READ_CONTACTS) && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Permission granted // Calling a method to actually load the contacts loadContacts(); } else { // Permission not granted Toast.makeText(MainActivity.this, "Access denied!", Toast.LENGTH_SHORT).show(); } return true; } return false; } }); }
注意:当您实现PermissionListener时,请不要忘记在requestCode是正确的时返回true,否则PermissionListener 将不会从ArrayList中删除,并且您很可能会得到一个小的内存泄漏.