项目中遇到了从相册选取图片裁剪后上传头像的功能,但在小米5 6.0系统的机型上无法裁剪,报错如下,
9-26 15:14:31.331 3828-3850/? E/ANDR-PERF-RESOURCEQS: Failed to apply optimization [4, 0]
09-26 15:14:31.634 2391-2391/? E/Icon: Unable to load resource 0x00000000 from pkg=com.android.systemui
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1374)
at android.content.res.MiuiResources.getValue(MiuiResources.java:145)
at android.content.res.Resources.getDrawable(Resources.java:827)
at android.graphics.drawable.Icon.loadDrawableInner(Icon.java:313)
at android.graphics.drawable.Icon.loadDrawable(Icon.java:269)
at android.graphics.drawable.Icon.loadDrawableAsUser(Icon.java:377)
at com.android.systemui.statusbar.ExpandedIcon.getDrawable(ExpandedIcon.java:59)
at com.android.systemui.statusbar.StatusBarIconView.getIcon(StatusBarIconView.java:174)
at com.android.systemui.statusbar.StatusBarIconView.setIcon(StatusBarIconView.java:131)
at com.android.systemui.statusbar.StatusBarIconView.updateDarkMode(StatusBarIconView.java:266)
at com.android.systemui.statusbar.phone.SimpleStatusBar.updateDarkMode(SimpleStatusBar.java:233)
at com.android.systemui.statusbar.phone.PhoneStatusBar$26.run(PhoneStatusBar.java:3169)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5458)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
09-26 15:14:31.701 3828-3850/? E/ANDR-PERF-OPTSHANDLER: perf_lock_rel: updated /sys/class/scsi_host/host0/../../../clkscale_enable with 1
return value 2
09-26 15:14:33.930 3828-3850/? E/ANDR-PERF-RESOURCEQS: Failed to apply optimization [4, 0]
09-26 15:14:33.970 10396-10396/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.wallet.newnetclient, PID: 10396
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3, result=-1, data=Intent { dat=file:///storage/emulated/0/DCIM/Camera/IMG_20161030_175839.jpg }} to activity {com.wallet.newnetclient/com.wallet.newnetclient.ui.activity.UserHeadActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3723)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3766)
at android.app.ActivityThread.access$1400(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1409)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5458)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference
at com.wallet.newnetclient.ui.activity.UserHeadActivity.onActivityResult(UserHeadActivity.java:123)
at android.app.Activity.dispatchActivityResult(Activity.java:6537)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3719)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3766)
at android.app.ActivityThread.access$1400(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1409)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5458)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
09-26 15:14:34.240 10695-10695/? E/awcn.SessionCenter: |[seq:umeng:58db8204677baa366b000a19] setDataReceiveCb AccsFrameCb:com.taobao.accs.net.j@ade633a
09-26 15:14:34.257 3828-3850/? E/ANDR-PERF-OPTSHANDLER: perf_lock_rel: updated /sys/class/scsi_host/host0/../../../clkscale_enable with 1
分析:本来以为是6.0动态获取权限问题,但通过网上查找资料才知道原来是因为定制系统小米手机在选择照片回调intent的数据格式和其他的手机不一样。
裁剪后的图片通过Intent的putExtra("return-data",true)方法进行传递,miui系统问题就出在这里,return-data的方式只适用于小图,miui系统默认的裁剪图片可能裁剪得过大,或对return-data分配的资源不足,造成return-data失败。
解决思路是:裁剪后,将裁剪的图片保存在Uri中,在onActivityResult()方法中,再提取对应的Uri图片转换为Bitmap使用。
解决前代码如下:
private Bitmap head;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
cropPhoto(data.getData());//裁剪图片
}
break;
case 2:
if (resultCode == RESULT_OK) {
File temp = new File(Environment.getExternalStorageDirectory()
+ "/head.jpg");
cropPhoto(Uri.fromFile(temp));//裁剪图片
}
break;
case 3:
if (data != null) {
Bundle extras = data.getExtras();
head = extras.getParcelable("data");//问题在这里
if(head!=null){
//上传服务器代码
try {
postUserHead2Server(head);
} catch (Exception e) {
e.printStackTrace();
}
setPicToView(head);//保存在SD卡中
Bitmap blur = StackBlur.blur(head, (int) 25, false);
Drawable drawable2 = new BitmapDrawable(blur);//转换成drawable
ivHead.setImageBitmap(head);
ivUserHeadBg.setBackground(drawable2);
}
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 调用系统的裁剪
* @param uri
*/
public void cropPhoto(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
intent.putExtra("return-data", true);//问题在这里
uritempFile=uri;
intent.putExtra(MediaStore.EXTRA_OUTPUT, uritempFile);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", false); // no face detection
startActivityForResult(intent, 3);
解决后,见代码如下:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
cropPhoto(data.getData());//裁剪图片
}
break;
case 2:
if (resultCode == RESULT_OK) {
File temp = new File(Environment.getExternalStorageDirectory()
+ "/head.jpg");
cropPhoto(Uri.fromFile(temp));//裁剪图片
}
break;
case 3:
if (data != null) {
Bundle extras = data.getExtras();
//处理小米手机不兼容问题
try {
head = BitmapFactory.decodeStream(getContentResolver().openInputStream(uritempFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(head!=null){
//上传服务器代码
try {
postUserHead2Server(head);
} catch (Exception e) {
e.printStackTrace();
}
setPicToView(head);//保存在SD卡中
Bitmap blur = StackBlur.blur(head, (int) 25, false);
Drawable drawable2 = new BitmapDrawable(blur);//转换成drawable
ivHead.setImageBitmap(head);
ivUserHeadBg.setBackground(drawable2);
}
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 调用系统的裁剪
* @param uri
*/
private Uri uritempFile;
public void cropPhoto(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
//处理小米手机不兼容问题
uritempFile=uri;
intent.putExtra(MediaStore.EXTRA_OUTPUT, uritempFile);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", false); // no face detection
startActivityForResult(intent, 3);
}
解决小米miui系统调用系统裁剪图片功能camera.action.CROP后崩溃或重新打开app的问题
点击打开链接 http://blog.csdn.net/eclothy/article/details/42719217