Android6.0动态权限适配

Android6.0动态权限适配

Android6.0开始,权限分为普通权限和危险权限,危险权限:会授予应用访问用户的机密数据的权限。如果你的应用在其清单文件中列出了危险权限,用户批准你的应用试验这些权限。
通过adb 命令

F:\sodaoProject\automate>adb shell
cepheus:/ $ pm list permissions -g -d
Dangerous Permissions:
// 涉及读写联系人,访问账户
group:android.permission-group.CONTACTS
  permission:android.permission.WRITE_CONTACTS
  permission:android.permission.GET_ACCOUNTS
  permission:android.permission.READ_CONTACTS
// 涉及电话操作
group:android.permission-group.PHONE
  permission:android.permission.READ_CALL_LOG
  permission:android.permission.READ_PHONE_STATE
  permission:android.permission.CALL_PHONE
  permission:android.permission.WRITE_CALL_LOG
  permission:android.permission.USE_SIP
  permission:android.permission.PROCESS_OUTGOING_CALLS
  permission:com.android.voicemail.permission.ADD_VOICEMAIL
// 涉及日历信息的操作(用户日程安排)
group:android.permission-group.CALENDAR
  permission:android.permission.READ_CALENDAR
  permission:android.permission.WRITE_CALENDAR
// 涉及相机操作
group:android.permission-group.CAMERA
  permission:android.permission.CAMERA
// 涉及使用手机传感器操作
group:android.permission-group.SENSORS
  permission:android.permission.BODY_SENSORS
// 涉及用户地理位置信息的操作
group:android.permission-group.LOCATION
  permission:android.permission.ACCESS_FINE_LOCATION
  permission:android.permission.ACCESS_COARSE_LOCATION
// 涉及存储卡的读写操作
group:android.permission-group.STORAGE
  permission:android.permission.READ_EXTERNAL_STORAGE
  permission:android.permission.WRITE_EXTERNAL_STORAGE
// 涉及多媒体信息的操作
group:android.permission-group.MICROPHONE
  permission:android.permission.RECORD_AUDIO
// 涉及SMS卡的操作
group:android.permission-group.SMS
  permission:android.permission.READ_SMS
  permission:android.permission.RECEIVE_WAP_PUSH
  permission:android.permission.RECEIVE_MMS
  permission:android.permission.RECEIVE_SMS
  permission:android.permission.SEND_SMS
  permission:android.permission.READ_CELL_BROADCASTS

ungrouped:
  permission:com.xiaomi.xmsf.permission.PAYMENT // 小米 下载而不显示通知
  permission:miui.permission.ACCESS_BLE_SETTINGS  // 小米 涉及到用户设置的操作
一:权限申请涉及到几个方法

1.检查权限

 ContextCompat.checkSelfPermission(Context context, String permission)
 例子:检测联系人权限
(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)

返回值(package android.content.pm的PackageManager中的常量)

1.  有权限: PackageManager.PERMISSION_GRANTED  值:0已授权
 
2.  无权限: PackageManager.PERMISSION_DENIED   值:-1未授权

2.解释权限

ActivityCompat.shouldShowRequestPermissionRationale(Activity activity, String permission)
例子:
ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_CONTACTS)~~~~

(1)判断是否有必要向用户解释为什么要这项权限,如果第一次请求此权限,但用户拒绝了,则之后调用该方法将返回true,此时有必要向用户详细说明需要此权限的原因
(2)如果应用第一次拒绝了请求此权限、第二次再请求此权限时,用户勾选了权限请求对话框“不在询问”,则此方法返回false.如果设备规范禁止应用拥有该权限,此方法返回也是false
3.请求权限

ActivityCompat.requestPermissions(Activity activity, String[] permissions, int requestCode)
例子:
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},MY_PERMISSIONS_REQUEST_READ_CONTACTS);

(1):权限参数传入的是数组,可以调用该方法一次请求多个权限;
(2):传入的权限数组参数以单个具体权限为单位,但弹框询问用户授权时,属于同一权限组的权限将自动合并询问授权一次;
(3):请求的权限必须事先在 AndroidManifest.xml 中有声明,否则调用此方法请求时,将不弹框,而是直接返回“拒绝”的结果;
(4):第一次请求权限时,用户点击了“拒绝”,第二次再请求该权限时,对话框将出现“不再询问”复选框,如果用户勾选了“不再询问”并点击了“拒绝”,则之后再请求此权限组时将不弹框,而是直接返回“拒绝”的结果。
4.处理请求结果

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
//重写请求结果
}
例子:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    //重写onRequestPermissionsResult方法根据用户的不同选择做出响应
 switch (requestCode){
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS:
            if (grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                Log.d(TAG,"onRequestPermissionsResult granted");
 }else {
                Log.d(TAG,"onRequestPermissionsResult denied");
 showWaringDialog();//提示设置开启权限弹窗
 }
            break;
 }

实现代码:

private void requestPermission() {
    Log.d(TAG,"requestPermission");
 //读取联系人的权限
 //判断权限是否授权
 if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
        Log.d(TAG,"未授权");
 //解释为什么需要授权该权限
 if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)){
            Log.d(TAG,"解释");
 //请求授权此权限
 ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},MY_PERMISSIONS_REQUEST_READ_CONTACTS);
 }else {
            Log.d(TAG,"没有解释");
 //请求授权此权限
 ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},MY_PERMISSIONS_REQUEST_READ_CONTACTS);
 }
    }else {
        Log.d(TAG,"已经授权");
 }
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    //重写onRequestPermissionsResult方法根据用户的不同选择做出响应
 switch (requestCode){
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS:
            if (grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                Log.d(TAG,"onRequestPermissionsResult granted");
 }else {
                Log.d(TAG,"onRequestPermissionsResult denied");
 showWaringDialog();
 }
            break;
 }
}
private void showWaringDialog() {
    AlertDialog dialog=new AlertDialog.Builder(this)
            .setTitle("提示!")
            .setMessage("请前往设置->应用->TestW->权限中打开相关权限,否则功能无法正常运行!")
            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
 public void onClick(DialogInterface dialog, int which) {
                    //一般情况下,如果用户不授权的话,功能是无法运行的,做出退出处理
 finish();
 }
            })
            .show();
}

编写一个权限申请工具类

public class PermissionUtils {
    private static final String TAG=PermissionUtils.class.getSimpleName();
 //九大危险权限dangerous
 //照相机
 public static final String PERMISSION_CAMERA= Manifest.permission.CAMERA;
 //读写文件
 public static final String PERMISSION_READ_EXTERNAL_STORAGE=Manifest.permission.READ_EXTERNAL_STORAGE;
 public static final String PERMISSION_WRITE_EXTERNAL_STORAGE=Manifest.permission.WRITE_EXTERNAL_STORAGE;
 //地理位置
 public static final String PERMISSION_ACCESS_FINE_LOCATION=Manifest.permission.ACCESS_FINE_LOCATION;
 public static final String PERMISSION_ACCESS_COARSE_LOCATION=Manifest.permission.ACCESS_COARSE_LOCATION;
 //电话
 public static final String PERMISSION_CALL_PHONE=Manifest.permission.CALL_PHONE;
 public static final String PERMISSION_READ_PHONE_STATE=Manifest.permission.READ_PHONE_STATE;
 //访问账户,读写联系人
 public static final String PERMISSION_GET_ACCOUNTS=Manifest.permission.GET_ACCOUNTS;
 //多媒体
 public static final String PERMISSION_RECORD_AUDIO=Manifest.permission.RECORD_AUDIO;
 //权限集合
 private static final String[] requestPermissions={
            PERMISSION_RECORD_AUDIO,
 PERMISSION_GET_ACCOUNTS,
 PERMISSION_READ_PHONE_STATE,
 PERMISSION_CALL_PHONE,
 PERMISSION_CAMERA,
 PERMISSION_ACCESS_FINE_LOCATION,
 PERMISSION_ACCESS_COARSE_LOCATION,
 PERMISSION_READ_EXTERNAL_STORAGE,
 PERMISSION_WRITE_EXTERNAL_STORAGE
 };
 public static final int CODE_RECORD_AUDIO = 0;
 public static final int CODE_GET_ACCOUNTS = 1;
 public static final int CODE_READ_PHONE_STATE = 2;
 public static final int CODE_CALL_PHONE = 3;
 public static final int CODE_CAMERA = 4;
 public static final int CODE_ACCESS_FINE_LOCATION = 5;
 public static final int CODE_ACCESS_COARSE_LOCATION = 6;
 public static final int CODE_READ_EXTERNAL_STORAGE = 7;
 public static final int CODE_WRITE_EXTERNAL_STORAGE = 8;
 public static final int CODE_MULTI_PERMISSION = 100;
 interface PermissionGrant {
        void onPermissionGranted(int requestCode);
 }
    //请求权限
 public static void requestPermission(final Activity activity,final int requestCode,PermissionGrant permissionGrant){
        if (activity==null){
            return;
 }
        Log.d(TAG,"requestPermission requestCode:" + requestCode);
 if (requestCode<0||requestCode>=requestPermissions.length){
            Log.w(TAG, "requestPermission illegal requestCode:" + requestCode);
 return; }
        final String requestPermission=requestPermissions[requestCode];
 int checkSelfPermission;
 try {
           checkSelfPermission= ActivityCompat.checkSelfPermission(activity,requestPermission);
 }catch (RuntimeException e){
           Toast.makeText(activity, "please open this permission", Toast.LENGTH_SHORT)
                   .show();
 Log.e(TAG, "RuntimeException:" + e.getMessage());
 return; }
       if (checkSelfPermission!= PackageManager.PERMISSION_GRANTED){
           Log.i(TAG, "ActivityCompat.checkSelfPermission != PackageManager.PERMISSION_GRANTED");
 if (ActivityCompat.shouldShowRequestPermissionRationale(activity,requestPermission)){
               Log.i(TAG, "requestPermission shouldShowRequestPermissionRationale");
 shouldShowRationale(activity, requestCode, requestPermission);
 }else {
               Log.d(TAG, "requestCameraPermission else");
 ActivityCompat.requestPermissions(activity,new String[]{requestPermission},requestCode);
 }
       }else {
           Log.d(TAG, "ActivityCompat.checkSelfPermission ==== PackageManager.PERMISSION_GRANTED");
 Toast.makeText(activity, "opened:" + requestPermissions[requestCode], Toast.LENGTH_SHORT).show();
 permissionGrant.onPermissionGranted(requestCode);
 }
    }
  

 private static void shouldShowRationale(Activity activity, int requestCode, String requestPermission) {
        String[] permissionsHint=activity.getResources().getStringArray(R.array.permissions);
 showMessageOkCancel(activity, "Rationale:" + permissionsHint[requestCode], new DialogInterface.OnClickListener() {
            @Override
 public void onClick(DialogInterface dialog, int which) {
                ActivityCompat.requestPermissions(activity,
 new String[]{requestPermission},
 requestCode);
 Log.d(TAG, "showMessageOKCancel requestPermissions:" + requestPermission);
 }
        });
 }
    private static void showMessageOkCancel(final Activity context, String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(context)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show();
 }
    public static void requestPermissionsResult(final Activity activity, final int requestCode, @NonNull String[] permissions,
 @NonNull int[] grantResults, PermissionGrant permissionGrant){
        if (activity==null){
            return;
 }
        Log.d(TAG, "requestPermissionsResult requestCode:" + requestCode);

        if (requestCode<0||requestCode>=requestPermissions.length){
            Log.w(TAG, "requestPermissionsResult illegal requestCode:" + requestCode);
 Toast.makeText(activity, "illegal requestCode:" + requestCode, Toast.LENGTH_SHORT).show();
 return; }
        Log.i(TAG, "onRequestPermissionsResult requestCode:" + requestCode + ",permissions:" + permissions.toString()
                + ",grantResults:" + grantResults.toString() + ",length:" + grantResults.length);
 if (grantResults.length==1&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
            permissionGrant.onPermissionGranted(requestCode);
 }else {
            String[] permissionsHint =activity.getResources().getStringArray(R.array.permissions);
 openSettingActivity(activity, "Result" + permissionsHint[requestCode]);
 }
    }
    
  //开启设置界面
 private static void openSettingActivity(Activity activity, String message) {
        showMessageOkCancel(activity, message, new DialogInterface.OnClickListener() {
            @Override
 public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent();
 intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
 Log.d(TAG, "getPackageName(): " + activity.getPackageName());
 Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
 intent.setData(uri);
 activity.startActivity(intent);
 }
        });
 }
}

在ThirdActivity的Activity中

public class ThirdActivity extends AppCompatActivity{
    private static final String TAG = ThirdActivity.class.getSimpleName();
 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_third);
 }
    public void showCamera(View view) {
        Log.i(TAG, "Show camera button pressed. Checking permission.");
 PermissionUtils.requestPermission(this, PermissionUtils.CODE_CAMERA, mPermissionGrant);
 }
    public void getAccounts(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_GET_ACCOUNTS, mPermissionGrant);
 }
    public void callPhone(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_CALL_PHONE, mPermissionGrant);
 }
    public void readPhoneState(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_READ_PHONE_STATE, mPermissionGrant);
 }
    public void accessFineLocation(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_ACCESS_FINE_LOCATION, mPermissionGrant);
 }
    public void accessCoarseLocation(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_ACCESS_COARSE_LOCATION, mPermissionGrant);
 }
    public void readExternalStorage(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_READ_EXTERNAL_STORAGE, mPermissionGrant);
 }
    public void writeExternalStorage(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_WRITE_EXTERNAL_STORAGE, mPermissionGrant);
 }
    public void recordAudio(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_RECORD_AUDIO, mPermissionGrant);
 }
    private PermissionUtils.PermissionGrant mPermissionGrant=new PermissionUtils.PermissionGrant() {
        @Override
 public void onPermissionGranted(int requestCode) {
            switch (requestCode) {
                case PermissionUtils.CODE_RECORD_AUDIO:
                    Toast.makeText(ThirdActivity.this, "Result Permission Grant CODE_RECORD_AUDIO", Toast.LENGTH_SHORT).show();
 break; case PermissionUtils.CODE_GET_ACCOUNTS:
                    Toast.makeText(ThirdActivity.this, "Result Permission Grant CODE_GET_ACCOUNTS", Toast.LENGTH_SHORT).show();
 break; case PermissionUtils.CODE_READ_PHONE_STATE:
                    Toast.makeText(ThirdActivity.this, "Result Permission Grant CODE_READ_PHONE_STATE", Toast.LENGTH_SHORT).show();
 break; case PermissionUtils.CODE_CALL_PHONE:
                    Toast.makeText(ThirdActivity.this, "Result Permission Grant CODE_CALL_PHONE", Toast.LENGTH_SHORT).show();
 break; case PermissionUtils.CODE_CAMERA:
                    Toast.makeText(ThirdActivity.this, "Result Permission Grant CODE_CAMERA", Toast.LENGTH_SHORT).show();
 break; case PermissionUtils.CODE_ACCESS_FINE_LOCATION:
                    Toast.makeText(ThirdActivity.this, "Result Permission Grant CODE_ACCESS_FINE_LOCATION", Toast.LENGTH_SHORT).show();
 break; case PermissionUtils.CODE_ACCESS_COARSE_LOCATION:
                    Toast.makeText(ThirdActivity.this, "Result Permission Grant CODE_ACCESS_COARSE_LOCATION", Toast.LENGTH_SHORT).show();
 break; case PermissionUtils.CODE_READ_EXTERNAL_STORAGE:
                    Toast.makeText(ThirdActivity.this, "Result Permission Grant CODE_READ_EXTERNAL_STORAGE", Toast.LENGTH_SHORT).show();
 break; case PermissionUtils.CODE_WRITE_EXTERNAL_STORAGE:
                    Toast.makeText(ThirdActivity.this, "Result Permission Grant CODE_WRITE_EXTERNAL_STORAGE", Toast.LENGTH_SHORT).show();
 break; default:
                    break;
 }
        }
    };
 @Override
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionUtils.requestPermissionsResult(this, requestCode, permissions, grantResults, mPermissionGrant);
 }
}

布局文件activity_third中


 
 
 
 
 

清单文件的申请权限


    
    
    
    
    

    
    
    
    
    

资源文件在res下values下创建一个array.xml文件


    
        @string/permission_recode_audio_hint
        @string/permission_get_accounts_hint
        @string/permission_read_phone_hint
        @string/permission_call_phone_hint
        @string/permission_camera_hint
        @string/permission_access_fine_location_hint
        @string/permission_access_coarse_location_hint
        @string/permission_read_external_hint
        @string/permission_white_external_hint
    

在res下values下的strings.xml文件创建


 非常建议您开启无障碍服务,开启后可以协助您完成一些简单的操作,提升您的操作体验。
 没有此权限,无法开启这个功能,请开启权限。PERMISSION_GET_ACCOUNTS
 没有此权限,无法开启这个功能,请开启权限。PERMISSION_READ_PHONE_STATE
 没有此权限,无法开启这个功能,请开启权限。PERMISSION_CALL_PHONE
 没有此权限,无法开启这个功能,请开启权限。PERMISSION_CAMERA
 没有此权限,无法开启这个功能,请开启权限。PERMISSION_ACCESS_FINE_LOCATION
 没有此权限,无法开启这个功能,请开启权限。PERMISSION_ACCESS_COARSE_LOCATION
 没有此权限,无法开启这个功能,请开启权限。PERMISSION_READ_EXTERNAL_STORAGE
 没有此权限,无法开启这个功能,请开启权限。PERMISSION_WRITE_EXTERNAL_STORAGE
 没有此权限,无法开启这个功能,请开启权限。PERMISSION_RECORD_AUDIO

小米系统在Android原生的动态权限申请的基础上,还有自己的用户授权模块
Android6.0动态权限适配_第1张图片
可以看到小米的授权模块中,对权限操作分为允许,询问,拒绝。
1.当我们第一次打开应用的时候,默认是询问状态,在该状态下,我们调用requestPermission()方法会弹出系统询问框.
2.在弹出系统授权框后,只要你操作了(拒绝或者允许),你永远也不要想着在以后能看到授权框了,除非你过来设置这边更改为“询问”模式。
3.不然无论你再调用几次requestPermissions(),都是直接走回调OnRequestPermissionResult。

Android7.0调用相机拍照保存照片,就是对手机存储中私有文件路径的保护

1.在Android7.0之前中调用相机实现拍照保存照片

/**
 * 调用系统相机实现拍照*/
public void request(View view){
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
        Toast.makeText(ThirdActivity.this,"SD卡异常",Toast.LENGTH_LONG).show();
 return; }
    long dateTaken=System.currentTimeMillis();
 //图像名称
 CharSequence fileName= DateFormat.format("yyyy-MM-dd kk.mm.ss",dateTaken);
 //图像路径
 String path=Environment.getExternalStorageDirectory().toString()+ File.separator+"forpermission"+File.separator+fileName+".jpg";
 File imageFile=new File(path);
 if (!imageFile.getParentFile().exists()){
        imageFile.getParentFile().mkdirs();
 }
    if (!imageFile.exists()){
        try {
            imageFile.createNewFile();
 } catch (IOException e) {
            e.printStackTrace();
 }
    }
    //根据文件解析出文件对应的Uri
 Uri uri=Uri.fromFile(imageFile); Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT,uri); //根据文件解析出文件对应的uri
 //判断是否有Activity能处理intent
 if (intent.resolveActivity(getPackageManager())!=null){
        startActivityForResult(intent,REQUEST_TAKE_PHOTO);
 }
}

2.返回结果

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
 if (resultCode==RESULT_OK){
        if (requestCode==REQUEST_TAKE_PHOTO){
            Toast.makeText(this, "拍照成功", Toast.LENGTH_SHORT).show();
 Log.d(TAG,"拍照成功了呀");
 }
    }
}

Android7.0之后如果调用系统相机拍照会报这个问题

Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/forpermission/2021-03-15%2014.30.48.jpg exposed beyond app through ClipData.Item.getUri()

需要提供FileProvider

/**
 * 调用系统相机实现拍照*/
public void request(View view){
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
        Toast.makeText(ThirdActivity.this,"SD卡异常",Toast.LENGTH_LONG).show();
 return; }
    long dateTaken=System.currentTimeMillis();
 //图像名称
 CharSequence fileName= DateFormat.format("yyyy-MM-dd kk.mm.ss",dateTaken);
 //图像路径
 String path=Environment.getExternalStorageDirectory().toString()+ File.separator+"forpermission"+File.separator+fileName+".jpg";
 File imageFile=new File(path);
 if (!imageFile.getParentFile().exists()){
        imageFile.getParentFile().mkdirs();
 }
    if (!imageFile.exists()){
        try {
            imageFile.createNewFile();
 } catch (IOException e) {
            e.printStackTrace();
 }
    }
 //根据文件解析出文件对应的uri
 Uri uri= FileProvider.getUriForFile(ThirdActivity.this,"com.ruan.testw.my_provider",imageFile);
 Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
 //判断是否有Activity能处理intent
 if (intent.resolveActivity(getPackageManager())!=null){
        startActivityForResult(intent,REQUEST_TAKE_PHOTO);
 }
}

对应的FileProvider在AndroidX和android中不同
在Androidmanifest中配置FileProvider


//是否允许授权文件的临时访问权限。传 true 表示需要 
 

//在android包下


 

在Android的res下创建xml文件夹下新建file_paths.xml文件



    
 


 
    


    


    



    



    
  • root-path:表示根目录,“/”。new File("/");
  • files-path:表示 content.getFileDir() 获取到的目录
  • cache-path:表示 content.getCacheDir() 获取到的目录
  • external-path:表示Environment.getExternalStorageDirectory() 指向的目录
  • external-files-path:表示 ContextCompat.getExternalFilesDirs() 获取到的目录
  • external-cache-path:表示 ContextCompat.getExternalCacheDirs() 获取到的目录

注意:
如果App有选择和剪裁图片的需求,最好配置下root-path,这样子可以读取到sd卡和一些应用分身的目录,否则微信等应用分身保存的图片,在App里面读取时就发生下面异常:

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/forpermission/2021-03-15 16.32.35.jpg

实例:


  
 

1.使用FileProvider.getUriForFile(ThirdActivity.this,"com.ruan.testw.my_provider",imageFile);
目的:是将file://转成content://
2.getUriForFile() 方法,需要一个 authority 的参数,这里需要与前面在 AndroidManifest.xml 中 配置的 android:authorities保持一致,因为是通过 android:authorities 属性配置的值,来唯一确定由谁来响应这个 provider 的 。在 AndroidManifest.xml 中配置 provider 的时候,需要保证 android:authorities 的值,在整个系统中的唯一性,否者安装的时候会抛出异常

*END:
学而不思则罔,思而不学则殆*
**

你可能感兴趣的:(android)