Android: App crashes on onActivityResult while using Camera Intent

后端 未结 8 1580
一整个雨季
一整个雨季 2020-12-11 14:40

I am using camera intent to capture images in my App. The problem my app crashes on Android 5.0.2 while using camera. I am using intent from fragment. Below is

相关标签:
8条回答
  • 2020-12-11 15:39

    I think, if you are using intent from fragment and if your using getActivity() make sure you have attached your fragment to your activity otherwise it may throw you null pointer. therefore in my case i used a workaround to get the context, which you can use it

    create a gobal variable View rootView and initialize it OnViewCreated() method and you can use accordingly.

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
        return;
    }
    
    if (requestCode == REQUEST_IMAGE_CAPTURE) {
    Log.d(UploadPicturesFragment.class.getSimpleName(),
                "IMAGE URI NOT NULL: " + (mHighQualityImageUri == null));
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap( rootView.getContext().getContentResolver(),
                    mHighQualityImageUri);
            DroomUtil.beginCrop(mHighQualityImageUri, rootView.getContext(), this, true, bitmap.getWidth(),
                    bitmap.getHeight());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }}
    
    0 讨论(0)
  • 2020-12-11 15:43
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
    

    That means mHighQualityImageUri is null, in all likelihood. This will occur if you failed to hold onto that Uri using onSaveInstanceState(). It is entirely possible that your process will be terminated while your app is in the background and the camera app is in the foreground.

    0 讨论(0)
提交回复
热议问题