I want to create a new folder when I capture images and store it there. But my captured images is always going to my default gallery folder and the new folder is not appearing in the gallery if I don't restart my cellular phone. I need to restart my phone first for the folder to appear and the images captured to appear in that folder every time I take a pic.
Intent intent_cam = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File imagesFolder = new File(android.os.Environment.getExternalStorageDirectory(), "Apps");
if (!imagesFolder.exists()) {
imagesFolder.mkdirs();
File image = new File(imagesFolder, "Apps_" + timeStamp + ".png");
Uri uriSavedImage = Uri.fromFile(image);
intent_cam.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(intent_cam, 1);
}
else {
File image = new File(imagesFolder, "Apps_" + timeStamp + ".png");
Uri uriSavedImage = Uri.fromFile(image);
intent_cam.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(intent_cam, 1);
}
Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Use folder creation condition like this.
if (!imagesFolder.exists() && imagesFolder.mkdirs())
What is logcat saying???
I solved my second problem about needing to restart using this line:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
Related
I've found ways that have supposedly worked for people to retrieve the uri/bitmap of the camera taken image.
But when I try and create a bitmap out of the non-null uri that I'm getting the file isn't created and doesn't exist.
this the the function which I use to open the camera intent(Which is copied from stackoverflow):
private void openBackCamera() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp + ".jpg";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
currentImageBase64 = storageDir.getAbsolutePath() + "/" + imageFileName;
File file = new File(currentImageBase64);
Log.d("2","file 1: " +file.exists());
Uri outputFileUri = FileProvider.getUriForFile(getApplicationContext(),"com.mydomain.fileprovider",file);
currentImageBase64 =outputFileUri+"";
Log.d("2",outputFileUri+" is the uri got from camera");
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
and this is how I handel my images:
Log.d("2","camera request!"+ currentImageBase64);
File imgFile = new File(currentImageBase64);
if(imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ivPreview.setImageBitmap(myBitmap);
}else{
Log.d("2","file does not exist");
}
note that the camera request log shows that currentImagesBase64 is not null and has a value "content://com.mydomain.fileprovider/name/Pictures/20190121_173332.jpg"
the file anyway, does not exist.
/**
edit: the problem was insufficent permissions, camera and read&write permissions were requested and the problem was resolved!
**/
Turn out Permissions were the problem, once I gave runtime camera read&write storage permissions the problem was fixed!
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.
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;
}
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-
Creating A .Nomedia File
Adding A Dot Prefix
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);
I am currently capturing and saving an image in a specific folder successfully and having a specific name that i choose. the problem is that the image is also being saved in the gallery having a different name. how can i delete the image in the gallery.
this is how i am capturing the image.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), this.getResources().getString(R.string.attachments_folder));
File image = new File(imagesFolder, "IM_" + timeStamp + ".jpeg");
Uri uriSavedImage = Uri.fromFile(image); intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(intent, 100);
I'm using the camera intent to captured images. But it's not showing when I check in on the gallery..
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
Intent intent_cam = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory() + File.separator + "App Photos");
Intent mediaScan = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
if (!imagesFolder.exists()) {
imagesFolder.mkdirs();
File image = new File(imagesFolder, "App_" + timeStamp + ".png");
Uri uriSavedImage = Uri.fromFile(image);
intent_cam.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
mediaScan.setData(uriSavedImage);
getActivity().sendBroadcast(mediaScan);
startActivityForResult(intent_cam, 0);
}
else if (imagesFolder.exists()) {
File image = new File(imagesFolder, "App_" + timeStamp + ".png");
Uri uriSavedImage = Uri.fromFile(image);
intent_cam.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
mediaScan.setData(uriSavedImage);
getActivity().sendBroadcast(mediaScan);
startActivityForResult(intent_cam, 0);
}
Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
And is it true that the sendBroadcast no longer works in API 19. (4.4)? Android How to use MediaScannerConnection scanFile
You are invoking the scan request before the picture has been captured. Recall that startActivityForResult() is asynchronous. Put your sendBroadcast() call in onActivityResult(), which will be triggered when the camera activity returns control to you.
This will work in all android versions:
MediaScannerConnection.scanFile(this, new String[]{file.getPath()}, new String[]{"image/jpeg"}, null);
I am trying to save photo and video like
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String pathMedia = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyImages/image_001.png";
Uri uriSavedImage = Uri.fromFile(new File(pathMedia));
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
but it doesn't work ( I pass through onResult and everything is Ok ) but there is no folder MyImages. How to force phone to save images to specific folder ( it needs to work on every model phone with Froyo or upper) ?
I added to manifest WRITE_EXTERNAL STORAGE and CAMERA privileges.
It's probably because you never created the folder, try this:
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs(); // <----
File image = new File(imagesFolder, "image_001.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);