Skip to content Skip to sidebar Skip to footer

Built-in Camera, Using The Extra Mediastore.extra_output Stores Pictures Twice (in My Folder, And In The Default)

I'm currently developing an app which uses the built-in Camera. I call this snippet by clicking a button : Intent intent = new Intent('android.media.action.IMAGE_CAPTURE'); //Inte

Solution 1:

Another way, tested on android 2.1, is take the ID or Absolute path of the gallery last image, then you can delete the duplicated image.

It can be done like that:

/**
 * Gets the last image id from the media store
 * @return
 */privateintgetLastImageId(){
    final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
    finalStringimageOrderBy= MediaStore.Images.Media._ID+" DESC";
    CursorimageCursor= managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy);
    if(imageCursor.moveToFirst()){
        intid= imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));
        StringfullPath= imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
        Log.d(TAG, "getLastImageId::id " + id);
        Log.d(TAG, "getLastImageId::path " + fullPath);
        imageCursor.close();
        return id;
    }else{
        return0;
    }
}

And to remove the file:

privatevoidremoveImage(int id) {
   ContentResolvercr= getContentResolver();
   cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media._ID + "=?", newString[]{ Long.toString(id) } );
}

This code was based on the post: Deleting a gallery image after camera intent photo taken

Solution 2:

While the answer from "Ilango J" provides the basic idea.. I thought I'd actually write in how I actually did it. The temporary file path that we were setting in intent.putExtra() should be avoided as it's a non standard way across different hardwares. On HTC Desire (Android 2.2) it did not work, And i've heard it works on other phones. It's best to have a neutral approach which works every where.

Please note that this solution (using the Intent) requires that the phone's SD Card is available and is not mounted onto the PC. Even the normal Camera app wouldn't work when the SD Card is connected to the PC.

1) Initiate the Camera Capture intent. Note, I disabled temporary file writes (non-standard across different hardware)

Intentcamera=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(camera , 0);

2) Handle callback and retrieve the captured picture path from the Uri object and pass it to step#3

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case CAPTURE_PIC: {
        if (resultCode == RESULT_OK && data != null) {
            UricapturedImageUri= data.getData();
            StringcapturedPicFilePath= getRealPathFromURI(capturedImageUri);
            writeImageData(capturedImageUri, capturedPicFilePath);
            break;
        }
    }
    }
}

public String getRealPathFromURI(Uri contentUri) {
    String[] projx = { MediaStore.Images.Media.DATA };
    Cursorcursor= managedQuery(contentUri, projx, null, null, null);
    intcolumn_index= cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

3) Clone and delete the file. See that I used the Uri's InputStream to read the content. The same can be read from the File of the capturedPicFilePath too.

publicvoidwriteImageData(Uri capturedPictureUri, String capturedPicFilePath) {

    // Here's where the new file will be writtenStringnewCapturedFileAbsolutePath="something" + JPG;

    // Here's how to get FileInputStream Directly.try {
        InputStreamfileInputStream= getContentResolver().openInputStream(capturedPictureUri);
        cloneFile(fileInputStream, newCapturedFileAbsolutePath);
    } catch (FileNotFoundException e) {
        // suppress and log that the image write has failed. 
    }

    // Delete original file from Android's GalleryFilecapturedFile=newFile(capturedPicFilePath);
    booleanisCapturedCameraGalleryFileDeleted= capturedFile.delete();
}

  publicstaticvoidcloneFile(InputStream currentFileInputStream, String newPath) {
    FileOutputStreamnewFileStream=null;

    try {

        newFileStream = newFileOutputStream(newPath);

        byte[] bytesArray = newbyte[1024];
        int length;
        while ((length = currentFileInputStream.read(bytesArray)) > 0) {
            newFileStream.write(bytesArray, 0, length);
        }

        newFileStream.flush();

    } catch (Exception e) {
        Log.e("Prog", "Exception while copying file " + currentFileInputStream + " to "
                + newPath, e);
    } finally {
        try {
            if (currentFileInputStream != null) {
                currentFileInputStream.close();
            }

            if (newFileStream != null) {
                newFileStream.close();
            }
        } catch (IOException e) {
            // Suppress file stream close
            Log.e("Prog", "Exception occured while closing filestream ", e);
        }
    }
}

Solution 3:

try this code:

Intentintent=newIntent("android.media.action.IMAGE_CAPTURE");
//Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);Stringpath= Environment.getExternalStorageDirectory().getAbsolutePath();
path += "/myFolder/myPicture.jpg";
Filefile=newFile( path );
//file.mkdirs();UrioutputFileUri= Uri.fromFile( file );
//String absoluteOutputFileUri = file.getAbsolutePath();

intent.putExtra("output", outputFileUri);
startActivityForResult(intent, 0);

Post a Comment for "Built-in Camera, Using The Extra Mediastore.extra_output Stores Pictures Twice (in My Folder, And In The Default)"