How to save taken image only into a folder, not DCIM folder

2.1k views Asked by At

I have a problem with the following script. It is saving the image in the folder i want it to but it is also saving it to DCIM folder, thing that i don't want. So what do i have to change to save it only into my folder?

**

  • save photo

**

private static File getOutputMediaFile(int type) {

        // External sdcard location
        File mediaStorageDir = new File(
        // Environment.getDataDirectory()(Environment.)
                Environment.getExternalStorageDirectory().toString()
                        + "/Android/data/com.itbstudios.shoppinglist/Shopping List");

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                        + IMAGE_DIRECTORY_NAME + " directory");
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + ".itb");
        } else {
            return null;
        }

        return mediaFile;
    }

**

  • take photo

**

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

        // start the image capture Intent
        startActivityForResult(intent, 100);

Thank you!

1

There are 1 answers

5
Dev 9 On BEST ANSWER

My strategy: Capture the photo as described in the question, save it into the internal memory (data/data/com.package...) After that delete it from the public folder (DCIM/MEDIA/100MEDIA) with the following method (delete last taken picture from that folder...):

private void deleteLastPhotoTaken() {

    String[] projection = new String[] {
            MediaStore.Images.ImageColumns._ID,
            MediaStore.Images.ImageColumns.DATA,
            MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
            MediaStore.Images.ImageColumns.DATE_TAKEN,
            MediaStore.Images.ImageColumns.MIME_TYPE };

    final Cursor cursor = getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, 
            null,null, MediaStore.Images.ImageColumns.DATE_TAKEN + "DESC");

    if (cursor != null) {
        cursor.moveToFirst();

        int column_index_data =  
                cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        String image_path = cursor.getString(column_index_data);

        File file = new File(image_path);
        if (file.exists()) {
            file.delete();
        }
    }
}