Android 7.0的拍照相册选择适配问题

只简述我发现问题的根源,有些是适配了7.0,会报权限失败问题,那是由于没有动态授权导致,接下来我一步一步给大家实现7.0适配,使用的是Kotlin语言

1.在mainfest标签里加入以下该权限

<uses-permissionandroid:name="android.permission.CAMERA" />

<uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2.在application下加入该

<provider

android:name="android.support.v4.content.FileProvider"

android:authorities="com.wocus.myapplication.fileprovider" //这里写自己的包名,后面固定

android:exported="false"

android:grantUriPermissions="true">

<meta-data

android:name="android.support.FILE_PROVIDER_PATHS"

android:resource="@xml/filepaths" />

<provider>

提供filepaths源码,在res目录下创建xml文件夹,在里面新建filepaths.xml文件

<resources>

         <paths>

<external-pathpath=""name="sdcard_files"/>

<external-files-pathpath=""name="camera_has_sdcard"/>

<files-pathpath=""name="camera_no_sdcard"/>

        <paths/>

<resources/>

3.实现拍照和选择相册裁剪的代码


a.新建3个file对象用于存储读取出来的图片

varcamerafile:File?=null//照相机File对象

vargalleryfile:File?=null//相册的File对象

varcropfile:File?=null//照相机的File对象

b.首先在开始调用拍照或者选择图片的时候要动态验证权限

@SuppressLint("NewApi")

fun requestReadExternalPermission(){

if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)

!= PackageManager.PERMISSION_GRANTED) {

if(shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {

}else{

requestPermissions(arrayOf(Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE),0)

}

}

}


//权限回调方法

override funonRequestPermissionsResult(requestCode: Int,permissions: Array,grantResults: IntArray) {

when(requestCode){

0->{

if(grantResults.size>0

&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

}else{

Toasty.error(baseContext,"授权失败").show()

}

}

}

super.onRequestPermissionsResult(requestCode,permissions,grantResults)

}


c.拍照的代码

/**

* 选择照相机拍照

*/

fungetCamera(){

varintent:Intent= Intent(MediaStore.ACTION_IMAGE_CAPTURE)

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){

img_paths=SimpleDateFormat("yyyyMMddHHmmss").format(Date())+".jpg"

varpath:File= getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)

camerafile= File(path,img_paths)

if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.N) {//7.0及以上

intent.putExtra(MediaStore.EXTRA_OUTPUT,FileProvider.getUriForFile(this,"com.wocus.myapplication.fileprovider",camerafile))

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)

}else{

intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(camerafile))

}

startActivityForResult(intent,100)

}else{

Toasty.error(this,"存储卡不可用!").show()

}

}


d.从相册选择图片的代码

/**

* 从相册选择照片

*/

fungetGallery(){

img_paths=SimpleDateFormat("yyyyMMddHHmmss").format(Date())+".jpg"

varpath:File= getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)

galleryfile= File(path,img_paths)

varintent:Intent= Intent(Intent.ACTION_GET_CONTENT)

intent.addCategory(Intent.CATEGORY_OPENABLE)

intent.setType("image/*")

if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){

intent.putExtra(MediaStore.EXTRA_OUTPUT,FileProvider.getUriForFile(baseContext,"com.wocus.myapplication.fileprovider",galleryfile))

intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

startActivityForResult(intent,200)

}else{

startActivityForResult(intent,200)

}

}

e.回传方法的代码

override funonActivityResult(requestCode: Int,resultCode: Int,data: Intent?) {

super.onActivityResult(requestCode,resultCode,data)

if(requestCode==100&& resultCode== Activity.RESULT_OK){//拍照回传

img_paths=SimpleDateFormat("yyyyMMddHHmmss").format(Date())+".jpg"

varpath:File= getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)

cropfile= File(path,img_paths)

if(Build.VERSION.SDK_INT>=24){

varinputUri:Uri=FileProvider.getUriForFile(baseContext,"com.wocus.myapplication.fileprovider",camerafile)

startPhotoZoom(inputUri)

}else{

varinputUri:Uri=Uri.fromFile(camerafile)

startPhotoZoom(inputUri)

}

}else if(requestCode==200&& resultCode== Activity.RESULT_OK){//选择照片回传

img_paths=SimpleDateFormat("yyyyMMddHHmmss").format(Date())+".jpg"

varpath:File= getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)

cropfile= File(path,img_paths)

if(Build.VERSION.SDK_INT>=24){

varimgUri:File= File(GetImagePath.getPath(baseContext,data!!.data))

vardataUri:Uri=FileProvider.getUriForFile(baseContext,"com.wocus.myapplication.fileprovider",imgUri)

startPhotoZoom(dataUri)

}else{

startPhotoZoom(data!!.data)

}

}else{//裁剪回传

varbm:Bitmap=BitmapFactory.decodeFile(cropfile!!.absolutePath)

img_up_photo.setImageBitmap(bm)

}

}

f.裁剪代码

/**

* 图片剪裁

*/

funstartPhotoZoom(inputUri:Uri){

if(inputUri==null)return

varintent:Intent= Intent("com.android.camera.action.CROP")

//sdk>=24

if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.N) {

varoutPutUri:Uri = Uri.fromFile(cropfile)

intent.setDataAndType(inputUri,"image/*")

intent.putExtra(MediaStore.EXTRA_OUTPUT,outPutUri)

intent.putExtra("noFaceDetection", false);//去除默认的人脸识别,否则和剪裁匡重叠

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)

}else{

varoutPutUri:Uri = Uri.fromFile(cropfile)

if(Build.VERSION.SDK_INT>= android.os.Build.VERSION_CODES.KITKAT) {

varurl = GetImagePath.getPath(this,inputUri)//这个方法是处理4.4以上图片返回的Uri对象不同的处理方法

intent.setDataAndType(Uri.fromFile(File(url)),"image/*")

}else{

intent.setDataAndType(inputUri,"image/*")

}

intent.putExtra(MediaStore.EXTRA_OUTPUT,outPutUri)

}

// 设置裁剪

intent.putExtra("crop","true")

// aspectX aspectY 是宽高的比例

intent.putExtra("aspectX",1)

intent.putExtra("aspectY",1)

// outputX outputY 是裁剪图片宽高

intent.putExtra("outputX",200)

intent.putExtra("outputY",200)

intent.putExtra("return-data", false)

intent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString())// 图片格式

startActivityForResult(intent,300)//这里就将裁剪后的图片的Uri返回了

}

g.分享一个图片相关工具类,网上很多人用的是这个

packagecom.wocus.myapplication.util;

importandroid.annotation.SuppressLint;

importandroid.content.ContentUris;

importandroid.content.Context;

importandroid.database.Cursor;

importandroid.net.Uri;

importandroid.os.Build;

importandroid.os.Environment;

importandroid.provider.DocumentsContract;

importandroid.provider.MediaStore;

public classGetImagePath {

//  4.4以上  content://com.android.providers.media.documents/document/image:3952

//  4.4以下  content://media/external/images/media/3951

@SuppressLint("NewApi")

public staticStringgetPath(finalContext context, finalUri uri) {

final booleanisKitKat = Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT;

// DocumentProvider

if(isKitKat && DocumentsContract.isDocumentUri(context,uri)) {

// ExternalStorageProvider

if(isExternalStorageDocument(uri)) {

finalString docId = DocumentsContract.getDocumentId(uri);

finalString[] split = docId.split(":");

finalString type = split[0];

if("primary".equalsIgnoreCase(type)) {

returnEnvironment.getExternalStorageDirectory() +"/"+ split[1];

}

}

// DownloadsProvider

else if(isDownloadsDocument(uri)) {

finalString id = DocumentsContract.getDocumentId(uri);

finalUri contentUri = ContentUris.withAppendedId(

Uri.parse("content://downloads/public_downloads"),Long.valueOf(id));

returngetDataColumn(context,contentUri, null, null);

}

// MediaProvider

else if(isMediaDocument(uri)) {

finalString docId = DocumentsContract.getDocumentId(uri);

finalString[] split = docId.split(":");

finalString type = split[0];

Uri contentUri =null;

if("image".equals(type)) {

contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

}else if("video".equals(type)) {

contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

}else if("audio".equals(type)) {

contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

}

finalString selection ="_id=?";

finalString[] selectionArgs =newString[]{

split[1]

};

returngetDataColumn(context,contentUri,selection,selectionArgs);

}

}

// MediaStore (and general)

else if("content".equalsIgnoreCase(uri.getScheme())) {

// Return the remote address

if(isGooglePhotosUri(uri))

returnuri.getLastPathSegment();

returngetDataColumn(context,uri, null, null);

}

// File

else if("file".equalsIgnoreCase(uri.getScheme())) {

returnuri.getPath();

}

return null;

}

//Android 4.4以下版本自动使用该方法

public staticStringgetDataColumn(Context context,Uri uri,String selection,

String[] selectionArgs) {

Cursor cursor =null;

finalString column ="_data";

finalString[] projection = {

column

};

try{

cursor = context.getContentResolver().query(uri,projection,selection,selectionArgs,

null);

if(cursor !=null&& cursor.moveToFirst()) {

final intindex = cursor.getColumnIndexOrThrow(column);

returncursor.getString(index);

}

}finally{

if(cursor !=null)

cursor.close();

}

return null;

}

/**

*@paramuriThe Uri to check.

*@returnWhether the Uri authority is ExternalStorageProvider.

*/

public static booleanisExternalStorageDocument(Uri uri) {

return"com.android.externalstorage.documents".equals(uri.getAuthority());

}

/**

*@paramuriThe Uri to check.

*@returnWhether the Uri authority is DownloadsProvider.

*/

public static booleanisDownloadsDocument(Uri uri) {

return"com.android.providers.downloads.documents".equals(uri.getAuthority());

}

/**

*@paramuriThe Uri to check.

*@returnWhether the Uri authority is MediaProvider.

*/

public static booleanisMediaDocument(Uri uri) {

return"com.android.providers.media.documents".equals(uri.getAuthority());

}

/**

*@paramuriThe Uri to check.

*@returnWhether the Uri authority is Google Photos.

*/

public static booleanisGooglePhotosUri(Uri uri) {

return"com.google.android.apps.photos.content".equals(uri.getAuthority());

}

}


就到这里就OK了,关于为什么会出现问题可以度娘,我这里提供相关解决方案,如大家有任何疑问,我的QQ752422962,联系我





最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容