Android - Inability to get the image from the camera on 4.2.2

I try to take a photo and get the file path by calling:

Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(camera_intent, Static.TAKE_PICTURE); 

and than:

 case Static.TAKE_PICTURE: if(resultCode == Activity.RESULT_OK){ if(data.getData() != null){ Uri selectedImage = data.getData(); String path = selectedImage.getPath(); if(path.contains("images/media")){ path = Static.getImageRealPathFromURI(getActivity().getBaseContext(),selectedImage); } } } break; 

Works fine on 4.1.2 Galaxy S3, but every time it crashes on 4.2.2 Nexus 10 s:

 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65642, result=-1, data=null} to activity {com.******.***/com.******.***.Main}: java.lang.NullPointerException 

If I try to do the same for the video:

 Intent video_intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); startActivityForResult(video_intent, Static.RECORD_VIDEO); 

works great. In fact, I have no idea why.

+4
source share
1 answer

This works for me:

 photoFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + Static.genereateFileName() + ".jpg"; File imageFile = new File(photoFilePath); Uri imageFileUri = Uri.fromFile(imageFile); Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); camera_intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri); startActivityForResult(camera_intent, Static.TAKE_PICTURE); 
0
source

Source: https://habr.com/ru/post/1485978/


All Articles