how to control/save images took with android camera programmatically? - android

i've been trying to create a camera-related app. i know that i could create it programmatically from top, but i would prefer using the one that the phone supports.
what i mean is, rather then creating the camera from 0, i would/could call the camera activity. after all, it provides all the system and gui that i needed.
however, the problem is i wanted the result/image took to be saved in a folder that i created before, rather then saving it in the default camera folder. and also renaming the image took that instant from the default 'image' to names that i preferred.
how do i control that ?

Try this. Here am saving picture to sdcard and also changing its name while saving.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"pic"+ String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);

If you look at the Camera applicaton source code, it allows for startActivityForResult(..) that can return the image back to you. This is ideally what you'd like to do.
As a Little hint:
MediaStore

Use below method to take picture, SD_CARD_TEMP_DIR is the path and the image name you want it to store. Hope it help
private void takePicture(){
SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory() + File.separator + "farmerImage"+CassavaPref.getInstance(this).getImageSuffix()+".jpg";
file =new File(SD_CARD_TEMP_DIR);
Intent takePictureFromCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureFromCameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(takePictureFromCameraIntent, 1111);
}

Related

Android Java: Get Bitmap in Gallery

I have a Bitmap saved in the directory: Environment.getExternalStorageDirectory()+ File.separator + "Images" in a subdirectory. With a fileManager i can find the image and open it. But in the Gallery of the Smartphone i can't find the Image.
How can i get it there?
To be make you file known by the Gallery you need to call the MediaScanner:
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
See also: https://www.grokkingandroid.com/adding-files-to-androids-media-library-using-the-mediascanner/
You can actually directly access the Pictures directory. To do this, use
getExternalStoragePublicDirectory(String type)
, where type is equal to Environment.DIRECTORY_PICTURES.
See http://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory%28java.lang.String%29 for more info.
That's cleaner than supposing that pictures are always stored in a directory called "Images"...
Now to force the Gallery to register the change, look at https://stackoverflow.com/a/15837638/2984973 .
To quote the answer, you want to broadcast an intent specifying your new picture, likewise:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(myNewFile)));

getFilesDir() stops camera from completing action

I am trying to take a picture inside my application and save it to
Android/data/com.androidproject/files/Camera/photo.png
I was using this code
private class ClickListener implements View.OnClickListener {
#Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(getActivity().getExternalFilesDir("Camera"),
"photo.png");
Uri outputFileUri = Uri.fromFile(file);
Log.v("FILE", "" + outputFileUri);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CALL_BACK);
}
}
and someone suggested that I change the line starting with File file = ... to
File file = new File(getActivity().getFilesDir() + File.separator + "Camera" + File.separator + "photo.png");
file.mkdirs();
Which changes the path directory to what I wanted, however it also prevents the camera from completing the action. Meaning I click my button, the camera loads, I press the blue circle to snap the photo, then when I go to click the checkmark it does nothing. I can reload and retake the photo, or press x and cancel but I cannot select the checkmark.
Any ideas?
edit
After playing with it for a few hours I noticed something. When I go to click the checkmark and complete the photo taking operation there is a photo created in the folder. The checkmark doesn't close out the screen and go back to my activity but it creates a dummy type file. It's size is 0kb and its named "1092-309403.jpeg" not "photo.png". This file is also saved under "InternalStorage/DCIM/Camera". It's never fully saved though as my application never technically finishes the operation. As soon as I press x the picture deletes and its like it never happened. I also tried cleaning my project but that doesn't work.
Camera is a third party application and so it can not access your application's private storage. You have to give path to public storage. More information is here Camera Intent not saving photo
You may have folder named photo.png as leftover from your previous attempts. This prevents Camera app from creating a file with the same name. Check Android/data/com.androidproject/files/Camera/ on the external storage and if there is a folder named photo.png there, delete it.
I've a similar problem too, seems Android doesn't like internal storage... By the way, try with createTempFile():
File dir = new File(getActivity().getFilesDir() + File.separator + "Camera" );
File file = File.createTempFile( "photo.png", ".jpg", dir );

How to get an image picker only from DCIM directory

I have to include an image picker in my android app.
I set it like this :
Intent intentGallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intentGallery, 0);
But all the pictures of the SD card are displayed. I only want to show the pictures taken with the camera (in the DCIM folder).
Is it possible to do that ?
No, it is not possible.
If necessary, you can implement your own gallery, that will pick images only from
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI
and
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI
You would use android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI to access images on the device instead of the SD card.
[EDIT]
Browsing the Android source, specifically the Gallery app, I stumbled across
public static final String CAMERA_IMAGE_BUCKET_NAME = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera";
Which I guess is what you are looking for. You can view the full source here

How do I prevent a duplicate picture from adding to the gallery?

I use the following code to take a picture:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path + "/" + fileName)));
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
Now when I use this, it does save the picture where I specify above, but it also saves a second copy to the default image folder and adds it to the gallery.
I would simply delete the second file, but it seems it would be a tad dangerous as onActivityResult's intent parameter is always null after taking said picture, so I would have to attempt deleting the most recently saved picture.
Is there any way I can prevent this behavior or correct it by getting the URI of the dupicate picture?
Well I've determined that it is pretty much not possible. I am now using a SurfaceView with my own camera activity.

Camera intent/Activity - avoid saving to gallery

I am using the Camera Activity to capture the picture. I call it with the MediaStore.EXTRA_OUTPUT extra parameter. The image is correctly saved to provided path, put it is saved also to the gallery folder, so I can view the image in "Gallery" application - can I avoid this?
...
File file = new File(Environment.getExternalStorageDirectory(), "Test.jpg" );
iImageOutputUri = Uri.fromFile( file );
// Start camera intent to capture image
Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, iImageOutputUri );
startActivityForResult( intent, CAPTURE_IMAGE );
...
Thanks
Just replace your code
File file = new File(Environment.getExternalStorageDirectory(), "Test.jpg" );
with following code
File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "Test.jpg");
Description of getExternalFilesDir() :
Returns the absolute path to the directory on the primary external filesystem (that is somewhere on Environment.getExternalStorageDirectory()) where the application can place persistent files it owns. These files are internal to the applications, and not typically visible to the user as media.
How about saving your images with non-image extension ? Something like michael.photo instead of michael.jpg/png etc.That way Gallery app wont take them in and I have seen many such apps keepsafe for example does this.
I don't believe you can avoid this. The gallery application looks through your SD card for images and displays them automatically.

Categories

Resources