5

when i use this code ->

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

I am able to save the image to specified path but it is also saving to the gallery. I dont want to save the image to gallery. Please help here.

Thanks in advance for your valuable time.

Rakesh
  • 756
  • 1
  • 9
  • 19
Umakant Angadi
  • 79
  • 2
  • 10

3 Answers3

0

Capture Image using below Intent -

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

// create a file to save the image
File MyDir = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir");
if (!MyDir.exists()){
    MyDir.mkdirs()
}

File fileUri = new File(MyDir.getAbsolutePath() + File.separator + "IMG_"+ timeStamp + ".jpg"); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

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

The gallery app scans the folders for Media contents and populates them in gallery.

If you wish not to display your captured images in gallery follow below methods-

  1. Creating A .Nomedia File

  2. Adding A Dot Prefix

kevz
  • 2,727
  • 14
  • 39
  • 1
    Hi.. when i check in file manager i found same image stored in two folder 1)DCIM folder 2)URI which i specified – Umakant Angadi Mar 02 '16 at 06:48
  • @UmakantAngadi: check my edit. I have added code to capture image from camera Intent. Hope it helps. – kevz Mar 02 '16 at 06:57
  • 1
    thanks for ur code. Here I am facing issue 1) it is still saving that image into /storage/emulated/0/DCIM/Camera/20160302_143206.jpg 2) I am saving that image in /storage/emulated/0/sample/IMG_20160302_143158.png The same image in 2 folder with different file names. I dont think phone is scanning folders and creating the same image with different file name. My question is: phone by default creating the image and storing into DCIM folder(gallery). This should not happen. Thanks in advance. – Umakant Angadi Mar 02 '16 at 09:13
  • @UmakantAngadi: Absolutely not. It doesn't scans and create duplicate file in DCIM. It simply displays the image from the your folder into gallery – kevz Mar 02 '16 at 09:18
  • @UmakantAngadi: Hey u can check this links http://stackoverflow.com/questions/8078892/stop-saving-photos-using-android-native-camera – kevz Mar 02 '16 at 09:23
0

try this

private void captureCameraImage() {

        Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, getFilename());

        startActivityForResult(chooserIntent, CAMERA_PHOTO);

    }

method to return file name that you can specify whre you want to save

public String getFilename() {
    File file = new File(Environment.getExternalStorageDirectory().getPath(), "MyFolder/Images");
    if (!file.exists()) {
        file.mkdirs();
    }
    String uriSting = (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".jpg");
    return uriSting;

}
saeed
  • 1,935
  • 1
  • 18
  • 33
0

I faced the same problem and implemented several workarounds similar to this one. I tried also to keep the file hidden adding the . prefix to the filname and to put a .nomedia file (see MediaStore.MEDIA_IGNORE_FILENAME) within the folder where I stored the images but in some cases, calling the camera app via intent as usual

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
    Uri fileUri = FileProvider.getUriForFile(getContext(), getString(R.string.file_provider_authority), file);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    startActivityForResult(intent, CAMERA_REQUEST_CODE);
} else {
    showToastMessage(getString(R.string.no_camera_activity), Toast.LENGTH_LONG);
}

depending on the device and the camera app, this latter might store the picture also within the gallery (usually saving a file with a timestamp as the filename) even if you are providing an Uri associated to a file you are storing within your application private partition.

So I found that the most reliable way of doing what I needed was to control the camera directly by your own or to adopt cwac-cam2 library provided by CommonsWare within YourActivity in this way (note the commented .updateMediaStore() line)

Uri fileUri = FileProvider.getUriForFile(getContext(), getString(R.string.file_provider_authority), file);
CameraActivity.IntentBuilder builder = new CameraActivity.IntentBuilder(this); // this refers to the activity instance
    Intent intent = builder
            .skipConfirm()
            .facing(Facing.BACK)
            .to(fileUri)
            //.updateMediaStore() // uncomment only if you want to update MediaStore
            .flashMode(FlashMode.AUTO)
            .build();
    startActivityForResult(intent, CAMERA_REQUEST_CODE);
Community
  • 1
  • 1
bardi
  • 373
  • 5
  • 17