Images duplicated when take image using own camera inside app

351 views Asked by At

By using a camera intent to take a picture in my app, images was duplicated from SD card and Gallery. Here my code to take picture:

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            // Ensure that there's a camera activity to handle the intent
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                // Create the File where the photo should go
                File photoFile = null;
                try {
                    photoFile = "file to /storage/emulated/0/Pictures/folder/image.jpg"
                } catch (IOException ex) {
                    // Error occurred while creating the File
                    Log.e("Can't create file", ex.getLocalizedMessage());
                }

                Uri photoUri = Uri.fromFile(photoFile);

                // Continue only if the File was successfully created
                if (photoFile != null) {
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
                    startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);

                }

Image saved to photoFile and another same saved to Gallery. How to resolved it?

2

There are 2 answers

1
Wes1324 On

I assume that you want the image saved in the SD card but not in the gallery? If so, try saving the photo in the app's private space in the external memory of the device rather than in the public Pictures directory. I did it in my app like this:

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File photoFile = File.createTempFile(imageFileName, ".jpg", storageDir);
0
sathishvels On

I have used the timestamp to save images in local file and to avoid the image duplicated

ContentValues contentValues = new ContentValues();
            ContentResolver resolver = getContentResolver();
            String timeStamp = new SimpleDateFormat( "yyyyMMdd_HHmmss", Locale.US).format( new Date() );
            contentValues.put( MediaStore.MediaColumns.DISPLAY_NAME, "Image_" + timeStamp + ".jpg" );
            contentValues.put( MediaStore.MediaColumns.MIME_TYPE, "image/jpeg" );
            contentValues.put( MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + File.separator + "ENTER YOUR FILE NAME" );
            imageUri = resolver.insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues );