Android:将操作系统版本更新为5.1.1时,Image Crop崩溃

前端之家收集整理的这篇文章主要介绍了Android:将操作系统版本更新为5.1.1时,Image Crop崩溃前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我将我的nexus 5 Android操作系统版本更新为5.1.1,并更新了Google相机和Google相册应用程序.在此之后,当我尝试捕获图像并裁剪它时,我的应用程序崩溃并出现以下错误
  1. FATAL EXCEPTION: main
  2. Process: com.app.test,PID: 4857
  3. java.lang.RuntimeException: Failure delivering result ResultInfo{who=null,request=2,result=-1,data=Intent { typ=image/jpeg }} to activity {com.app.test/com.app.test.newActivity.activities.TestActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference
  4. at android.app.ActivityThread.deliverResults(ActivityThread.java:3574)
  5. at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
  6. at android.app.ActivityThread.access$1300(ActivityThread.java:151)
  7. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
  8. at android.os.Handler.dispatchMessage(Handler.java:102)
  9. at android.os.Looper.loop(Looper.java:135)
  10. at android.app.ActivityThread.main(ActivityThread.java:5254)
  11. at java.lang.reflect.Method.invoke(Native Method)
  12. at java.lang.reflect.Method.invoke(Method.java:372)
  13. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
  14. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
  15. 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
  16. at com.app.test.newActivity.activities.TestActivity.onActivityResult(TestActivity.java:127)
  17. at android.app.Activity.dispatchActivityResult(Activity.java:6192)
  18. at android.app.ActivityThread.deliverResults(ActivityThread.java:3570)
  19. at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
  20. at android.app.ActivityThread.access$1300(ActivityThread.java:151)
  21. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
  22. at android.os.Handler.dispatchMessage(Handler.java:102)
  23. at android.os.Looper.loop(Looper.java:135)
  24. at android.app.ActivityThread.main(ActivityThread.java:5254)
  25. at java.lang.reflect.Method.invoke(Native Method)
  26. at java.lang.reflect.Method.invoke(Method.java:372)
  27. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
  28. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

以前它工作正常.我使用的代码如下:

图像捕获代码

  1. try {
  2. Intent imageCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  3. if (imageCapture.resolveActivity(getContext().getPackageManager()) != null) {
  4. imageCapture.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + Constants.image_path)));
  5. startActivityForResult(imageCapture,Constants.CAMERA_IMAGE_CAPTURE);
  6. }
  7. } catch (ActivityNotFoundException anfe) {
  8. Toast.makeText(getContext(),"device doesn't support capturing images!",Toast.LENGTH_SHORT).show();
  9. }

图像裁剪代码

  1. @Override
  2. protected void onActivityResult(int requestCode,int resultCode,Intent intent) {
  3. super.onActivityResult(requestCode,resultCode,intent);
  4. if (resultCode == RESULT_OK) {
  5. if (requestCode == CAMERA_IAMGE_CROP) {
  6. Bundle extras = intent.getExtras();//intent.getExtras() is always returns NULL here
  7. Bitmap thePic = extras.getParcelable("data");
  8. //setImageOnImageView(thePic);
  9. } else if (requestCode == Constants.CAMERA_IMAGE_CAPTURE)) {
  10. processCapturedImage();
  11. }
  12. }
  13. }
  14.  
  15. private void processCapturedImage() {
  16. try {
  17. String path = Environment.getExternalStorageDirectory().getAbsolutePath() + Constants.image_path;
  18. File file = new File(path);
  19. if (file.exists()) {
  20. BitmapFactory.Options options = new BitmapFactory.Options();
  21. options.inPreferredConfig = Bitmap.Config.RGB_565;
  22. Bitmap bm = BitmapFactory.decodeFile(path,options);
  23. int rotate = AndroidUtils.getRotateValue(file.getAbsolutePath());
  24. if (rotate != 0) {
  25. Debug.print("Profile pic rotation value is not 0.");
  26. /****** Image rotation ****/
  27. Matrix matrix = new Matrix();
  28. matrix.postRotate(rotate);
  29. bm = Bitmap.createBitmap(bm,bm.getWidth(),bm.getHeight(),matrix,true);
  30. }
  31. picUri = getImageUri(getApplicationContext(),bm);
  32. performCropAction();
  33. } else {
  34. Tools.showToast(EditProfileActivity.this,"Error occurred,please try again.");
  35. }
  36. } catch (Exception e) {
  37. Debug.printException(e);
  38. }
  39. }
  40.  
  41. private void performCropAction() {
  42. try {
  43. Intent cropAction = new Intent("com.android.camera.action.CROP");
  44. cropAction.setDataAndType(picUri,"image/*");
  45. cropAction.putExtra("crop","true");
  46. cropAction.putExtra("aspectX",1);
  47. cropAction.putExtra("aspectY",1);
  48. cropAction.putExtra("outputX",AS.getInPixels(100));
  49. cropAction.putExtra("outputY",AS.getInPixels(100));
  50. cropAction.putExtra("return-data",true);
  51. startActivityForResult(cropAction,CAMERA_IAMGE_CROP);
  52. }
  53. catch (ActivityNotFoundException anfe) {
  54. Toast.makeText(this,"your device doesn't support the crop action!",Toast.LENGTH_SHORT).show();
  55. }
  56. }

如你看到的,
Bundle extras = intent.getExtras();
这里的intent.getExtras()始终返回NULL.

任何帮助真的很感激!
谢谢.

解决方法

以上android 5.0裁剪功能在onActivityResult中返回URI,因此请相应地使用手机版本处理它.
  1. Bitmap selectedBitmap;
  2. if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  3. Bundle extras = data.getExtras();
  4. selectedBitmap = extras.getParcelable("data");
  5. }
  6. else{
  7. Uri uri = data.getData();
  8. selectedBitmap=MediaStore.Images.Media.getBitmap(this.getContentResolver(),uri);
  9. }

希望这可以帮助!

猜你在找的Android相关文章