关于Android6.0/7.0权限,以及7.0调用相机和切图的解决办法

来源:互联网 发布:foreach去除数组重复值 编辑:程序博客网 时间:2024/06/05 20:05

Android高版本出现的一些问题,如题现在贴出结局办法:

1.  权限申请

建议在你的BaseActivity中去增加两个方法。

public void onPermissionRequests(String permission, OnBooleanListener onBooleanListener) {    onPermissionListener = onBooleanListener;    Log.d("MainActivity", "0");    if (ContextCompat.checkSelfPermission(this,            permission)            != PackageManager.PERMISSION_GRANTED) {        // Should we show an explanation?        Log.d("MainActivity", "1");        if (ActivityCompat.shouldShowRequestPermissionRationale(this,                Manifest.permission.READ_CONTACTS)) {            //权限已有            onPermissionListener.onClick(true);        } else {            //没有权限,申请一下            ActivityCompat.requestPermissions(this,                    new String[]{permission},                    1);        }    }else{        onPermissionListener.onClick(true);        Log.d("MainActivity", "2"+ContextCompat.checkSelfPermission(this,                permission));    }}@Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {    if (requestCode == 1) {        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {            //权限通过            if (onPermissionListener != null) {                onPermissionListener.onClick(true);            }        } else {            //权限拒绝            if (onPermissionListener != null) {                onPermissionListener.onClick(false);            }        }        return;    }    super.onRequestPermissionsResult(requestCode, permissions, grantResults);}

然后在你需要申请权限的地方调用这个方法就可以了

onPermissionRequests
2.  7.0相机问题

首先是准备工作,先在res文件夹下添加xml文件夹,然后创建一个文件file_paths,内容如下:

<?xml version="1.0" encoding="utf-8"?><paths xmlns:android="http://schemas.android.com/apk/res/android">    <!--填写你要所要申请访问的目录地址,name最好是你的目录名,path是你要申请的目录-->    <external-path name="camera_photos" path="."  />    <external-path name="cache" path="Android/data/com.xuezj.fileproviderdemo/cache" />    <external-path name="images" path="Pictures/" />    <external-path name="dcim" path="DCIM/" /></paths>
然后在AndroidManifest.xml文件中添加

<!-- 这里直接复制就可以,需要注意的是authorities,以你的包名加上fileprovider,因为他需要唯一 --><provider    android:name="android.support.v4.content.FileProvider"    android:authorities="com.xuezj.fileproviderdemo.fileprovider"    android:exported="false"    android:grantUriPermissions="true">    <meta-data        android:name="android.support.FILE_PROVIDER_PATHS"        android:resource="@xml/file_paths"/></provider>

这样准备工作就做好了,不要纠结着FileProvider,到时候直接用就可以,下面是调用代码:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);File photoFile = createImagePathFile(MainActivity.this);intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);/** 这里就是高版本需要注意的,需用使用FileProvider来获取Uri,同时需要注意getUriForFile* 方法第二个参数要与AndroidManifest.xml中provider的里面的属性authorities的值一致* */intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);imageUriFromCamera = FileProvider.getUriForFile(MainActivity.this,        "com.xuezj.fileproviderdemo.fileprovider", photoFile);intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUriFromCamera);startActivityForResult(intent, GET_IMAGE_BY_CAMERA_U);

然后就没有然后了,我直接贴出我写的一个小demo的地址,里面又调用切图的功能,很简单:

https://github.com/xuezj/FileProviderDemo   点击打开链接


                                             
14 0
原创粉丝点击