Leonardo
Leonardo

Reputation: 539

Take photo of given size Android

I'm developing an application that takes a photo and saves it in Android/data/package/files. I would like to reduce storage used, so I would like to resize the photo before saving it. For the moment I'm calling new intent passing as extra the output path. Is possible to pass also the size wanted or is possible to have the bitmap before saving it?

public void takePicture(View view){
    Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File f = new File(getPath(nameEdit.getText().toString()));
    path = f.getAbsolutePath();
    pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
    if (pictureIntent.resolveActivity(getPackageManager())!=null){
        startActivityForResult(pictureIntent,REQUEST_IMAGE_CAPTURE);
    }
}

Upvotes: 1

Views: 109

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006584

Is possible to pass also the size wanted

No.

is possible to have the bitmap before saving it?

Not really. You could write your own ContentProvider and use a Uri for that, rather than a file. When the camera app tries saving the content, you would get that information in memory first. However, you will then crash with an OutOfMemoryError, as you will not have enough heap space to hold a full-resolution photo, in all likelihood.

You can use BitmapFactory with inSampleSize set in the BitmapFactory.Options to read in the file once it has been written by the camera, save the resized image as you see fit, then delete the full-resolution image. Or, skip EXTRA_OUTPUT, and you will get a thumbnail image returned to you, obtained by calling getExtra("data") on the Intent passed into onHandleIntent().

Upvotes: 2

Related Questions