How can I capture an image in Android and have it show up in the gallery?

user189350 picture user189350 · Oct 14, 2009 · Viewed 12.5k times · Source

I was capturing images before that were showing up in the gallery, but now they are not and I can't figure out why. Here is my code:

        ContentValues values = new ContentValues();
    values.put(android.provider.MediaStore.Images.Media.IS_PRIVATE, false);
    String name = "ugc_" + String.valueOf(System.currentTimeMillis());
    values.put(android.provider.MediaStore.Images.Media.TITLE, name);
    imageURI = getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

I've also tried: values.put(android.provider.MediaStore.Images.Media.IS_PRIVATE, 0); as well as leaving at that value, but nothing seems to work now.

Answer

I82Much picture I82Much · Oct 14, 2009

Very much not pretty, but this is how I'm doing it. Note that due to a bug, the file saved is 1/16 the full resolution (1/4 in each dimension).

String SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory() + File.separator + "tmpPhoto.jpg";
Intent takePictureFromCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureFromCameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new
            File(SD_CARD_TEMP_DIR)));
startActivityForResult(takePictureFromCameraIntent, TAKE_PICTURE_WITH_CAMERA);

. . .

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //  Picture taken from camera
        if (requestCode == TAKE_PICTURE_WITH_CAMERA) {
            if (resultCode == Activity.RESULT_OK) {

                // http://code.google.com/p/android/issues/detail?id=1480
                //Toast.makeText(AddPhotos.this, "" + data, Toast.LENGTH_SHORT).show();

                // on activity return
                File f = new File(SD_CARD_TEMP_DIR);
                try {
                    Uri capturedImage =
                        Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(),
                                f.getAbsolutePath(), null, null));


                        Log.i("camera", "Selected image: " + capturedImage.toString());

                    f.delete();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }
            else {
                Log.i("Camera", "Result code was " + resultCode);

            }
        }   
     }