Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking photos "simply" does not work

Adding file path extra to image capture intent causes camera app to malfunction on TF300t Android tablet with stock system version 4.2.1. Pressing "done" button does nothing - not even closing camera app activity. No result is returned.

The code I'm using was extracted from Adroid developers site

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imageFile = createImageFile();
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
startActivityForResult(cameraIntent, THIS_CAMERA_REQUEST);

With createImageFile() defined as:

private File createImageFile() throws IOException {
    File outputDir = getBaseContext().getCacheDir();

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "photo_" + timeStamp + "_";
    File image = new File(outputDir, imageFileName);

    return image;
}

When line

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));

is removed, camera app acts as expected.

Is there any resonable workaround ? I would rather not build a camera app myself just to take a photo.

like image 492
atok Avatar asked May 08 '13 11:05

atok


1 Answers

Problematic line:

File outputDir = getBaseContext().getCacheDir();

I've replaced it with:

private File createImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "1mind_" + timeStamp + ".jpg";
    File photo = new File(Environment.getExternalStorageDirectory(),  imageFileName);
    return photo;
}

Turns out, image has to be stored on external storage not in cache dir.

like image 107
atok Avatar answered Oct 27 '22 23:10

atok