onActivityResult returned from a camera, Intent null

前端 未结 7 742
执念已碎
执念已碎 2020-12-06 04:41

I follow the instruction on Camera on Android dev site

I just start the Camera Intent, not build my own camera.

The sample code to handle result return afte

相关标签:
7条回答
  • 2020-12-06 05:13

    Try this

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        switch (requestCode) {
            case PICK_IMAGE_ID:
    
                Bitmap bitmap = ImagePicker.getImageFromResult(this.getActivity(), resultCode, data);
    
                if(data!=null){
                    //set image view
                }
                break;
            default:
                super.onActivityResult(requestCode, resultCode, data);
                break;
        }
    }
    
    0 讨论(0)
  • 2020-12-06 05:17

    The problem with your code is this:

    data.getData()
    

    This call does not get the extra with the key "data" from the returned Intent. It gets the field data from the returned Intent which is probably null.

    You need to get the extra from the returned Intent like this:

    data.getExtras().get("data");
    

    Some of the other answers have indicated this, embedded in tons of other code. That makes it difficult to actually see what the problem is.

    0 讨论(0)
  • 2020-12-06 05:21

    Try this code below.....

    btn_capture.setOnClickListener(new OnClickListener() {
    
            public void onClick(View v) {
    
                Intent cameraIntent = new  Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                startActivityForResult(cameraIntent, CAMERA_REQUEST); 
            }
        });
     protected void onActivityResult(int requestCode, int resultCode, Intent data){
    
    
                 if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
                     Bitmap photo = (Bitmap) data.getExtras().get("data"); 
                     img.setImageBitmap(photo);
                 } 
              }
    
        }
    

    Then finally you add below code to your manifest

       <uses-feature android:name="android.hardware.camera"></uses-feature> 
    
    0 讨论(0)
  • 2020-12-06 05:34

    Here is the answer from a similar question. It seems like it might be a problem with Samsung phones...

    Basically, if you have code like this which creates the Intent:

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
    
    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    

    Then, in onActivityResult, replace data.getData() with fileUri.toString() and it will solve your problem.

    0 讨论(0)
  • 2020-12-06 05:37

    Use @KJ50's solution, and use savedInstanceState to make sure you don't get a null.

    /**
         * Here we store the file url as it will be null after returning from camera
         * app
         */
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
    
            // save file url in bundle as it will be null on screen orientation
            // changes
            outState.putParcelable("file_uri", fileUri);
        }
    
        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
    
            // get the file url
            fileUri = savedInstanceState.getParcelable("file_uri");
        }
    
    0 讨论(0)
  • 2020-12-06 05:37

    Try out below code :

     Button m_btnCamera;
      ImageView m_ivCaptureImage;
      String m_curentDateandTime;
      String m_imagePath;
      Bitmap m_bitmap;
    
       //Start camera to caputre image.
     Intent m_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      m_intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
      startActivityForResult(m_intent, TAKE_PHOTO_CODE);
    
     private Uri getImageUri() throws CustomException
        {
        Uri m_imgUri = null;
        File m_file;
        try
        {
            SimpleDateFormat m_sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
            m_curentDateandTime = m_sdf.format(new Date());
            m_imagePath = File.getPath() + File.separator + m_curentDateandTime + ".jpg";
            m_file = new File(m_imagePath);
            m_imgUri = Uri.fromFile(m_file);
        }
        catch (Exception p_e)
        {}      
        return m_imgUri;        
    }
    
     @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data)
      {
      super.onActivityResult(requestCode, resultCode, data);
     if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK)
        {
            m_bitmap = ImageHelper.scaleImage(m_imagePath, 200, 200);
                m_bitmap = ImageHelper.rotateImage(m_bitmap, true, m_rotate);
                m_ivCaptureImage.setImageBitmap(m_bitmap);
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题