Android get full size image from camera

15,254

I think the problem is that you are still trying to access the thumbnail from the Intent. In order to retrieve the full size image you need to access the file directly. So I'd recommend saving the file name before starting the camera activity and then loading the file at onActivityResult.

I suggest reading Taking Photos Simply page from the official documentation where you'll find this quote regarding the thumbnail:

Note: This thumbnail image from "data" might be good for an icon, but not a lot more. Dealing with a full-sized image takes a bit more work.

Also in the very last section, you'll find the code you need:

private void setPic() {
    // Get the dimensions of the View
    int targetW = mImageView.getWidth();
    int targetH = mImageView.getHeight();

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    mImageView.setImageBitmap(bitmap);
}
Share:
15,254
amburnside
Author by

amburnside

Web Developer PHP - Laravel 5.x / Zend framework Ruby on Rails MySQL LAMP / LEMP HTML5 CSS JS JQuery / Bootstrap

Updated on June 05, 2022

Comments

  • amburnside
    amburnside almost 2 years

    I am developing an Android App which uploads an image from the camera or from the device photo gallery to remote site. The latter I have working fine, I can select and upload. However, I am have trouble taking a full size image and uploading. This is my code:

    // From onCreate
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
    

    I have a method to handle the Activity result. This both handles selecting from the gallery and from the camera:

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    
      String filepath = ""; 
      Uri selectedImageUri;
    
      if (resultCode == RESULT_OK) {
        if (requestCode == CAMERA_PIC_REQUEST) {
          Bitmap photo = (Bitmap) data.getExtras().get("data");
          // Gets real path of image so it can be uploaded
          selectedImageUri = getImageUri(getApplicationContext(), photo);
        }
        else {
          selectedImageUri = data.getData();
        }
        // Handle the upload ...
      }
    }
    
    public Uri getImageUri(Context inContext, Bitmap inImage) {
      String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
      return Uri.parse(path);
    }
    

    This works however, the image is small and does not follow the naming convention for stored images. I have read that to save full size I need to use putExtra and pass MediaStore.EXTRA_OUTPUT on the cameraIntent and declare a temporary file, so I have adapted my intent code as follows:

    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
    camImgFilename = sdf.format(new Date())+".jpg";
    
    File photo = new File Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), camImgFilename);
    
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
    

    This however results in an error being thrown stating "Source not found".

    Not sure where to proceed from here?

    UPDATE

    It appears that my photo is being created. Once the app has closed I can navigate with the file explorer to the directory and see the image. I'm unsure of what is causing the app to crash.