onActivityResult returned from a camera, Intent null

前端 未结 7 746
执念已碎
执念已碎 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: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);
        }
      }
    }
    

提交回复
热议问题