Android6.0权限问题,并撰写常用权限工具类

在android6.0手机调用系统相机的时候,报如下错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{cn.doolii.shopping/cn.doolii.shopping.view.uploadImage.AlbumUploadActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/thumbnails from pid=27138, uid=10103 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2453)
在拍照的时候报如上问题,原因是没有添加相机权限
<uses-permission android:name="android.permission.CAMERA" />
同时android6.0以上系统新出权限问题
解决方法如下:
一:定义常量
public final static int MY_PERMISSIONS_REQUEST_READ_CONTACTS=1;
二:
在调用需要权限的方法之前,如调用拍照之前先判断是否有权限.
调用如下方法:
  1. @TargetApi(Build.VERSION_CODES.M)  
  2. private void getPermission() {  
  3.     if (ContextCompat.checkSelfPermission(UploadPicActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {  
  4.         //申请CAMERA的权限  
  5.         requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_READ_CONTACTS);  
  6.     } else {  
  7.         photo();  
  8.         pop.dismiss();  
  9.             ll_popup.clearAnimation();  
  10.     }  
  11. }  

权限返回结果
  1. @Override  
  2. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {  
  3.     super.onRequestPermissionsResult(requestCode, permissions, grantResults);  
  4.     switch (requestCode) {  
  5.         case MY_PERMISSIONS_REQUEST_READ_CONTACTS:  
  6.             if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {  
  7.                photo();  
  8.             } else {  
  9.                 Toast.makeText(UploadPicActivity.this"请打开相机权限", Toast.LENGTH_LONG).show();  
  10.             }  
  11.             break;  
  12.         }  
  13. }  

工具类如下:
  1. <pre name="code" class="java">package cn.doolii.usershopping.utils;  
  2.   
  3. import android.Manifest;  
  4. import android.annotation.TargetApi;  
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.content.pm.PackageManager;  
  8. import android.os.Build;  
  9. import android.support.v4.app.ActivityCompat;  
  10. import android.support.v4.app.Fragment;  
  11. import android.support.v4.content.ContextCompat;  
  12. import android.util.Log;  
  13.   
  14. import java.util.ArrayList;  
  15. import java.util.List;  
  16.   
  17. /**  
  18.  * Created by lzq on 2016/6/30.  
  19.  */  
  20. public class PermissionUtils {  
  21.     static Activity context;  
  22.   
  23.     public PermissionUtils(Activity context) {  
  24.         this.context=context;  
  25.     }  
  26.   
  27.     public static final int MY_PERMISSIONS_REQUEST_CALL_PHONE=200;  
  28.     public static void needPermission(int requestCode)  
  29.     {  
  30.         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M){  
  31.             return;  
  32.         }  
  33.         requestAllPermissions(requestCode);  
  34.     }  
  35.     private static void requestAllPermissions( int requestCode)  
  36.     {  
  37.             ActivityCompat.requestPermissions(context,  
  38.                     new String[]{Manifest.permission.CALL_PHONE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.READ_CONTACTS, Manifest.permission.GET_ACCOUNTS, Manifest.permission.ACCESS_FINE_LOCATION},  
  39.                     MY_PERMISSIONS_REQUEST_CALL_PHONE);  
  40.   
  41.     }  
  42.     private static boolean requesCallPhonePermissions( int requestCode)  
  43.     {  
  44.         if (ContextCompat.checkSelfPermission(context,  
  45.                 Manifest.permission.CALL_PHONE)  
  46.                 != PackageManager.PERMISSION_GRANTED)  
  47.         {//没有权限  
  48.             ActivityCompat.requestPermissions(context,  
  49.                     new String[]{Manifest.permission.CALL_PHONE},  
  50.                     MY_PERMISSIONS_REQUEST_CALL_PHONE);  
  51.             return false;  
  52.         } else  
  53.         {  
  54.             return true;  
  55.         }  
  56.     }  
  57.   
  58.     private static boolean requestReadSDCardPermissions( int requestCode)  
  59.     {  
  60.         if (ContextCompat.checkSelfPermission(context,  
  61.                 Manifest.permission.READ_EXTERNAL_STORAGE)  
  62.                 != PackageManager.PERMISSION_GRANTED)  
  63.         {//没有权限  
  64.             ActivityCompat.requestPermissions(context,  
  65.                     new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},  
  66.                     MY_PERMISSIONS_REQUEST_CALL_PHONE);  
  67.             return false;  
  68.         } else  
  69.         {  
  70.             return true;  
  71.         }  
  72.     }  
  73.     private static boolean requestCamerPermissions( int requestCode)  
  74.     {  
  75.         if (ContextCompat.checkSelfPermission(context,  
  76.                 Manifest.permission.CAMERA)  
  77.                 != PackageManager.PERMISSION_GRANTED)  
  78.         {//没有权限  
  79.             ActivityCompat.requestPermissions(context,  
  80.                     new String[]{Manifest.permission.CAMERA},  
  81.                     MY_PERMISSIONS_REQUEST_CALL_PHONE);  
  82.             return false;  
  83.         } else  
  84.         {  
  85.             return true;  
  86.         }  
  87.     }  
  88.     private static boolean requestReadConstantPermissions( int requestCode)  
  89.     {  
  90.         if (ContextCompat.checkSelfPermission(context,  
  91.                 Manifest.permission.READ_CONTACTS)  
  92.                 != PackageManager.PERMISSION_GRANTED)  
  93.         {//没有权限  
  94.             ActivityCompat.requestPermissions(context,  
  95.                     new String[]{Manifest.permission.READ_CONTACTS},  
  96.                     MY_PERMISSIONS_REQUEST_CALL_PHONE);  
  97.             return false;  
  98.         } else  
  99.         {  
  100.             return true;  
  101.         }  
  102.     }  
  103.     private static boolean requestGET_ACCOUNTSPermissions( int requestCode)  
  104.     {  
  105.         if (ContextCompat.checkSelfPermission(context,  
  106.                 Manifest.permission.GET_ACCOUNTS)  
  107.                 != PackageManager.PERMISSION_GRANTED)  
  108.         {//没有权限  
  109.             ActivityCompat.requestPermissions(context,  
  110.                     new String[]{Manifest.permission.GET_ACCOUNTS},  
  111.                     MY_PERMISSIONS_REQUEST_CALL_PHONE);  
  112.             return false;  
  113.         } else  
  114.         {  
  115.             return true;  
  116.         }  
  117.     }  
  118.     private static boolean requestLocationPermissions( int requestCode)  
  119.     {  
  120.         if (ContextCompat.checkSelfPermission(context,  
  121.                 Manifest.permission.ACCESS_FINE_LOCATION)  
  122.                 != PackageManager.PERMISSION_GRANTED)  
  123.         {//没有权限  
  124.             ActivityCompat.requestPermissions(context,  
  125.                     new String[]{Manifest.permission.ACCESS_FINE_LOCATION},  
  126.                     MY_PERMISSIONS_REQUEST_CALL_PHONE);  
  127.             return false;  
  128.         } else  
  129.         {  
  130.             return true;  
  131.         }  
  132.     }  
  133. }  


工具类使用方法如下:
我是在app第一次进入的时候,先判断android的版本,如果大于6.0,不管用户是否给与权限,都调用申请常用权限,防止因为用户不给于权限而导致崩溃,如:
存储,电话,相机,通讯录,位置等。防止用户
  1. if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){  
  2.     new PermissionUtils(this).needPermission(200);  
  3. }  
然后在接下来的需要调用这些权限时调用相对应的方法,如打电话时调用
requesCallPhonePermissions(200),参数随便传入。注意:此处的200 要与onRequestPermissionsResult中的参数requestCode一致。
重写请求权限后的回调方法:
  1. @Override  
  2. public void onRequestPermissionsResult(int requestCode,  
  3.                               String permissions[], int[] grantResults) {  
  4.    switch (requestCode) {  
  5.       case 200: {  
  6.          // If request is cancelled, the result arrays are empty.  
  7.          if (grantResults.length > 0  
  8.                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {  
  9.   
  10.             // permission was granted, yay! Do the  
  11.             // contacts-related task you need to do.  
  12.         //同意给与权限  可以再此处调用拍照  
  13.             Log.i("用户同意权限""user granted the permission!");  
  14.   
  15.          } else {  
  16.   
  17.             // permission denied, boo! Disable the  
  18.             // f用户不同意 可以给一些友好的提示  
  19.             Log.i("用户不同意权限""user denied the permission!");  
  20.          }  
  21.          return;  
  22.       }  
  23.   
  24.       // other 'case' lines to check for other  
  25.       // permissions this app might request  
  26.    }  
  27. }  

发布了0 篇原创文章 · 获赞 0 · 访问量 3538
展开阅读全文

关于Android的错误:Permission Denial: 。。。.READ_EXTERNAL_STORAGE, or grantUriPermission(

10-11

这是错误代码 ,在虚拟机运行时崩溃, E/AndroidRuntime: FATAL EXCEPTION: main Process: com.add.mp3list, PID: 7193 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.add.mp3list/com.add.mp3list.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=7193, uid=10090 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=7193, uid=10090 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() at android.os.Parcel.readException(Parcel.java:1942) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) at android.content.ContentProviderProxy.query(ContentProviderNative.java:418) at android.content.ContentResolver.query(ContentResolver.java:754) at android.content.ContentResolver.query(ContentResolver.java:704) at android.content.ContentResolver.query(ContentResolver.java:662) at com.add.mp3list.MusicList.getMusicData(MusicList.java:26) at com.add.mp3list.MainActivity.onCreate(MainActivity.java:21) at android.app.Activity.performCreate(Activity.java:6975) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6541)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)  Application terminated. 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览