前提
項目中使用的動態(tài)權(quán)限開源庫github:https://github.com/yanzhenjie/AndPermission
轉(zhuǎn)載必須注明本文轉(zhuǎn)自嚴振杰的博客:http://blog.csdn.net/yanzhenjie1003
添加依賴:compile 'com.yanzhenjie:permission:1.0.3'
Android6.0時代:
眾所周知,Android6.0時相機攝像頭權(quán)限改成了動態(tài)權(quán)限申請。實際上在xml中加入CAMERA,WRITE_EXTERNAL_STORAGE全向后,直接調(diào)用攝像頭忠售。此時是沒有“檢查權(quán)限是否授予”琼懊,“沒有授予再申請權(quán)限”的代碼的浪谴。
但是(重點),我發(fā)現(xiàn)
- 在VIVO跛梗,華為等國產(chǎn)機會彈出對話框,
- 三星透绩,sony等外國機不會有彈窗翘骂,調(diào)用攝像頭直接崩潰,
- 魅族手機沒有彈出帚豪,但是可以直接用攝像頭碳竟。
我猜測是VIVO,華為定制系統(tǒng)幫助用戶檢查并申請了相機權(quán)限狸臣,外國機則沒有瞭亮,魅族可能直接授予權(quán)限.為統(tǒng)一,建議android6.0每次都檢查并申請相機權(quán)限固棚,如下.
* 申請相機權(quán)限
*
* @param context
* @param photoFromCamera 拍照保存圖片路徑
*
*
* @see {https://github.com/yanzhenjie/AndPermission}
* */
public static void requestCameraPermission(final Context context, final String photoFromCamera){
//API >=23
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
AndPermission.with(context)
.requestCode(PERMISSION_MEDIA_REQUEST_CODE)
.permission(Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE)
.rationale(new RationaleListener() {
@Override
public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
// 此對話框可以自定義统翩,調(diào)用rationale.resume()就可以繼續(xù)申請。
AndPermission.rationaleDialog(context, rationale).show();
}
})
.callback(new PermissionListener() {
@Override
public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
// 權(quán)限申請成功回調(diào)此洲。
if(requestCode == PERMISSION_MEDIA_REQUEST_CODE) {
UIRouter.JumpToCameraActivity(context,photoFromCamera);
}
}
@Override
public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
// 權(quán)限申請失敗回調(diào)厂汗。
if(requestCode == PERMISSION_MEDIA_REQUEST_CODE) {
ToastView.showToast(context,"拒絕授權(quán)");
}
}
})
.start();
}
}
/**
* 調(diào)用系統(tǒng)拍照
* @param saveImagePathFromCamera 拍照圖片保存路徑
* @param context
*/
public static void JumpToCameraActivity(Context context, String saveImagePathFromCamera) {
/*調(diào)用系統(tǒng)拍照*/
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri = null;
File imageFile = FileUtil.getFile(saveImagePathFromCamera);//此路徑可以為storage/mounted/0/DCIM或其他外部存儲路徑
uri = Uri.fromFile(imageFile); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); ((Activity) context).startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
Android7.0以上:
測試過程中,發(fā)現(xiàn)在android7.0以上的設(shè)備上在按照如上Android6.0的方案使用攝像頭時呜师,直接崩潰掉了娶桦。原因是android7.0開始,相機拍照的圖像保存路徑必須在此應用的內(nèi)部存儲文件夾(storage/mounted/0/Android/data/包名//files/pictures文件夾)汁汗。需要使用FileProvider獲取內(nèi)部文件的uri
/**
* 申請相機權(quán)限
*
* @param context
* @param photoFromCamera 拍照保存圖片路徑
*
*
* @see {https://github.com/yanzhenjie/AndPermission}
* */
public static void requestCameraPermission(final Context context, final String photoFromCamera){
//API <23
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
UIRouter.JumpToCameraActivity(context,photoFromCamera);
}else {
//API >=23
AndPermission.with(context)
.requestCode(PERMISSION_MEDIA_REQUEST_CODE)
.permission(Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE)
.rationale(new RationaleListener() {
@Override
public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
// 此對話框可以自定義衷畦,調(diào)用rationale.resume()就可以繼續(xù)申請。
AndPermission.rationaleDialog(context, rationale).show();
}
})
.callback(new PermissionListener() {
@Override
public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
// 權(quán)限申請成功回調(diào)知牌。
if(requestCode == .PERMISSION_MEDIA_REQUEST_CODE) {
UIRouter.JumpToCameraActivity(context,photoFromCamera);
}
}
@Override
public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
// 權(quán)限申請失敗回調(diào)祈争。
if(requestCode ==PERMISSION_MEDIA_REQUEST_CODE) {
ToastView.showToast(context,"拒絕授權(quán)");
}
}
})
.start();
}
}
/**
* 調(diào)用系統(tǒng)拍照
*
* @param context
*/
public static void JumpToCameraActivity(Context context, String saveImagePathFromCamera) {
/*調(diào)用系統(tǒng)拍照*/
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri = null;
try {
File imageFile = FileUtil.getFile(saveImagePathFromCamera);
//API>=24 android 7.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
if (intent.resolveActivity(context.getPackageManager()) != null){
String imageName = imageFile.getName();
//7.0以上 的拍照文件必須在storage/emulated/0/Android/data/包名/files/pictures文件夾
File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File file = FileUtil.getFile(storageDir+"/"+imageName);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加這一句表示對目標應用臨時授權(quán)該Uri所代表的文件
uri = FileProvider.getUriForFile(context,"包名.fileprovider",file);
}
}else {//<24
uri = Uri.fromFile(imageFile);
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
((Activity) context).startActivityForResult(intent, CAMERA_REQUEST_CODE);
} catch (IOException e) {
e.printStackTrace();
}
}
1.在manifest.xml中加入:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="包名.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
2.在res中新建xml文件夾,創(chuàng)建file_paths.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<external-path
name="camera_photos"<!--任意-->
path="Android/data/包名/files/Pictures" /><!--相機圖片保存圖片路徑角寸,屬于APP的存儲空間-->
</paths>
</resources>