How to tell the device's camera device by default not to take the largest possible image, but the smaller one?

I use the device camera like this:

private void checkWhetherCameraIsAvailableAndTakeAPicture() {
    // Check wether this device is able to take pictures
    if (isIntentAvailable(a, MediaStore.ACTION_IMAGE_CAPTURE)) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File imageFile = null;
        try {
            imageFile = createImageFile();
            imagePath = imageFile.getAbsolutePath();
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
            takePictureIntent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, 1*1024*1024L); // Limit image to 1MB
        } catch (IOException e) {
            e.printStackTrace();
            imageFile = null;
            imagePath = null;
        }
        startActivityForResult(takePictureIntent, Preferences.REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA);
    }
}

      

And then the camera opens and I take the image and I get a 50MB bitmap file to work with and trying to work with it results in a massive OutOfMemoryError.

As you can see, I tried to limit the file size returned to 1MB, but apparently it doesn't work.

Now I know that if I use the Camera.openCamera () object, I can set whatever parameters I like there, but how can I tell that the device camera takes up less of a smaller image than the maximum size?

+3


source to share


2 answers


As you can see, I tried to limit the file size returned to 1MB, but apparently it doesn't work.

More accurately:

  • EXTRA_SIZE_LIMIT

    not documented for use with ACTION_IMAGE_CAPTURE

  • Camera apps don't need to perform any special functions, and there are thousands of camera apps.



how can I tell that the device's camera takes up less of a smaller image than the maximum size?

Not. Your choice:

  • Provide EXTRA_OUTPUT

    and receive what the camera app wants to give you (usually the maximum resolution, although this can be user-configured on certain cameras)

  • Don't provide EXTRA_OUTPUT

    and receive what the camera app wants to do (theoretically reduced image size)

+1


source


At first glance, it seems very easy to use the default camera app to capture an image, but there are many pitfalls. I wrote a little helper to deal with all the different Android and Android vendor implementations: https://github.com/ralfgehrer/AndroidCameraUtil

Also, there is a great library for loading and caching images: https://github.com/nostra13/Android-Universal-Image-Loader



If you are merging two libraries, you don't need to worry about file size, etc.

+1


source







All Articles