Android Camera Intent: how to get full sized photo?

前端 未结 7 1539
遇见更好的自我
遇见更好的自我 2020-11-22 16:35

I am using intent to launch camera:

Intent cameraIntent = new Intent(
    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
getParent().startActivityForResu         


        
7条回答
  •  甜味超标
    2020-11-22 16:53

    I also used the answer from Vicky but I had to save the uri to a bundle to avoid loss of it on orientation change. So if you don't get a result from your intent after tilting the device it might be because your uri did not survive the orientation change.

    static final int CAMERA_CAPTURE_REQUEST = 1;
    static final String ARG_CURRENT_PIC_URI = "CURRENT_PIC_URI";
    
    
    String pictureImagePath = folderName + "/" + imageFileName;
    File file = new File(Environment.getExternalStorageDirectory(), pictureImagePath);
    Uri outputFileUri = Uri.fromFile(file);
    
    mCurrentPicUri = outputFileUri.getPath();
    
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(cameraIntent, CAMERA_CAPTURE_REQUEST);
    

    Activity Result code:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    
      if (requestCode == CAMERA_CAPTURE_REQUEST && resultCode == Activity.RESULT_OK) 
      {
        File imgFile = new  File(mCurrentPicUri);
        // do something with your image
        // delete uri
        mCurrentPicUri = "";
      }
    }
    

    save the uri to the bundle:

    @Override
    public void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      // save uri to bundle
      outState.putString(ARG_CURRENT_PIC_URI, mCurrentPicUri);
    }
    

    retrieve it from your saved bundle during on create:

    if (bundle.containsKey(ARG_CURRENT_PIC_URI))
      mCurrentPicUri = bundle.getString(ARG_CURRENT_PIC_URI);
    

提交回复
热议问题