android 上传头像遇到的问题,android 7.0 图片剪切问题,选择头像上传 -凯发k8官方网
一、 7.0文件地址传输的问题
异常信息:fileuriexposedexception:
android:name="android.support.v4.content.fileprovider"android:authorities="应用报名.fileprovider"android:exported="false"android:granturipermissions="true">android:name="android.support.file_provider_paths"android:resource="@xml/file_paths"/>
在res.下的xml文件:
文件名是:file_paths(和清单文件中一致)
xml version="1.0"encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android">name="files_root"path="android/data/com.lechuang.letaotao/"/>name="external_storage_root"path="."/>
在 传输过程中提供图片地址:
public staticuri geturiforfile(context context,file file) {
if(context == null|| file == null) {
throw newnullpointerexception();}
uri uri;if(build.version.sdk_int>= 24) {
uri = fileprovider.geturiforfile(context,"应用包名.fileprovider",file);} else{
uri = uri.fromfile(file);}
returnuri;}
打开照相机:
//android 7.0// uri imageuri = fileprovider.geturiforfile(mcontext, "应用包名.fileprovider", file);
// 7.0 以
// uri uri = uri.fromfile(file);
//适配7.0和7.0一下的地址uri imageuri =
geturiforfile(
mcontext
,
file)
;intent intent =
newintent()
;intent.addflags(intent.
flag_grant_read_uri_permission)
;intent.setaction(mediastore.
action_image_capture)
;intent.putextra(mediastore.
extra_output
,imageuri)
; //把拍照后的图片放在指定的文件intent.putextra(mediastore.
extra_video_quality
,
0)
;startactivityforresult(intent
,
2)
;
相册选择:
intent intent = newintent(intent.action_pick,mediastore.images.media.external_content_uri);intent.setdataandtype(mediastore.images.media.external_content_uri,"image/*"); //*代表全部startactivityforresult(intent,1);
裁剪图片:
public static voidresizeimage(uri uri,activity activity, intrequestcode) {
intent intent = newintent("com.android.camera.action.crop");intent.setdataandtype(uri,"image/png"); //限定图片类型intent.addflags(intent.flag_grant_read_uri_permission); //重要 ,权限要求。intent.putextra("crop","true");
// aspectx , aspecty :宽高的比例intent.putextra("aspectx",1);intent.putextra("aspecty",1);
// outputx , outputy : 裁剪图片宽高intent.putextra("outputx",150);intent.putextra("outputy",150);intent.putextra("return-data", true);activity.startactivityforresult(intent,requestcode);}
压缩图片:
private voidshowresizeimage(intent data) {
bundle extras = data.getextras();if(extras != null) {
bitmap bitmap = extras.getparcelable("data");string path = uri.fromfile(file).getpath();bitmap =tosmall(bitmap,path);
}
}
压缩处理的具体方法:
public staticbitmap tosmall(bitmap bitmap,string path) {
intscale = 1;intwidth_tmp = bitmap.getwidth();intheight_tmp = bitmap.getheight();while(true) {
if(width_tmp / 2< 100|| height_tmp / 2< 100)
break;width_tmp /= 2;height_tmp /= 2;scale *= 2;}
matrix matrix = newmatrix();matrix.postscale(scale,scale);// 长和宽放大缩小的比例bitmap = bitmap.createbitmap(bitmap,0,0,bitmap.getwidth(),bitmap.getheight(),matrix, true);try{
fileoutputstream fileoutputstream = newfileoutputstream(path);try{
bitmap.compress(bitmap.compressformat.png,100,fileoutputstream);} finally{
try{
fileoutputstream.flush();fileoutputstream.close();} catch(ioexception e) {
}
}
} catch(filenotfoundexception e) {
e.printstacktrace();}
returnbitmap;}
主界面的处理:
@overridepublic voidonactivityresult(intrequestcode, intresultcode,intent data) {
switch(requestcode) {
case1:
if(resultcode != -1) {
return;}
.resizeimage(data.getdata(), this,3); //处理从相册返回的信息:break;case2:
if(resultcode != -1) {
return;}
if(photoutil.issdcardexisting()) { //处理从照相机返回的图片
uri uriforfile = geturiforfile(mcontext,file);resizeimage(uriforfile, this,3); //开始裁剪} else{
utils.show(mcontext,"未找到存储卡,无法存储照片");}
break;case3:
if(data != null) {
showresizeimage(data); // 压缩图片}
break;default:
break;}
super.onactivityresult(requestcode,resultcode,data);}
附 android6.0 文件和照相机权限申请:
private static final intrequest_permission_camera= 222;private static final intrequest_permission_storage= 333;private voidsetcamear() {
if(build.version.sdk_int>= build.version_codes.m) {
intcheckcallphonepermission = contextcompat.checkselfpermission(mcontext,manifest.permission.camera);if(checkcallphonepermission != packagemanager.permission_granted) {
activitycompat.requestpermissions(this, newstring[]{manifest.permission.camera},request_permission_camera);return;}
}
if(build.version.sdk_int>= build.version_codes.m) {
intcheckcallphonepermission = contextcompat.checkselfpermission(mcontext,manifest.permission.write_external_storage);if(checkcallphonepermission != packagemanager.permission_granted) {
activitycompat.requestpermissions(this, newstring[]{manifest.permission.write_external_storage},request_permission_storage);return;}
}
}
@overridepublic voidonrequestpermissionsresult(intrequestcode,string[] permissions, int[] grantresults) {
switch(requestcode) {
//就像onactivityresult一样这个地方就是判断你是从哪来的。caserequest_permission_camera:
if(grantresults[0] == packagemanager.permission_granted) {
} else{utils.show(mcontext,"很遗憾你把相机权限禁用了!");finish();}
break;caserequest_permission_storage:
if(grantresults[0] == packagemanager.permission_granted) {
} else{
// permission deniedutils.show(mcontext,"很遗憾你把读取文件权限禁用了!");finish();}
break;default:
super.onrequestpermissionsresult(requestcode,permissions,grantresults);}
}
与50位技术专家面对面20年技术见证,附赠技术全景图总结
以上是凯发k8官方网为你收集整理的android 上传头像遇到的问题,android 7.0 图片剪切问题,选择头像上传的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: android volley post
- 下一篇: