Captured image not appearing in gallery - android

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);

Related

Retrieving camera intent image full image uri

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!

Creating new folder when capturing images

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())));

Take a picture from camera

i have a problem with code of take a picture and save. It crash when i launchCamera().
Can you help me please?
private void launchCamera() {
try {
mOutputFile = File.createTempFile("prova", null);
Intent intentCamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intentCamera.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(mOutputFile));
startActivityForResult(intentCamera, CAMERA_REQUEST);
} catch (Exception e) {
Toast t = Toast.makeText(this, "ERROR:\n" + e.toString(), Toast.LENGTH_LONG);
t.show();
}
}
I am using this piece of code try it out :
/**
* This method is used to start the camera activity and save the image taken as the imagename passed
*
* #param imagename : this is the name of the image which will be saved
*/
private void clickPicture(String imagename) {
Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");
File cameraFolder;
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"myfolder/");
else
cameraFolder= context.getCacheDir();
if(!cameraFolder.exists())
cameraFolder.mkdirs();
String imageFileName = imagename;
File photo = new File(Environment.getExternalStorageDirectory(), "myfolder/" + imageFileName);
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
Uri.fromFile(photo);
startActivityForResult(getCameraImage, 1);
}
and add the permisson in your manifest file :
<uses-permission android:name="android.permission.CAMERA" ></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Maybe you didn't added the necessary permissions in the manifest file:
<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" />
Also I would suggest you to go over this blog post I wrote on this topic of taking a picture using the build-in Camera Activity:
Take Picture with build in Camera Activity
To access the device camera, you must declare the CAMERA permission in your Android Manifest

How to save image captured with camera in specific folder

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);

Unable to create new directory inside the Gallery folder

I am making an app which allows users to record audio clips and access them. I am trying to save the recorded audio files inside a custom folder in my gallery.
I have a function that creates a File and sets its location:
private File getOutputMediaFile(int type) {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "dirName" + File.separator);
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_" + timeStamp + ".jpg");
} else if (type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_" + timeStamp + ".mp4");
} else if (type == MEDIA_TYPE_AUDIO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"AUD" + timeStamp + ".WAV");
}
else {
return null;
}
return mediaFile;
}
I use getOutputMediaFile to save recorded audio file like so:
audioRecorder = new MediaRecorder();
audioFilename = getOutputMediaFile(MEDIA_TYPE_AUDIO).getAbsolutePath();
//more code
audioRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_AUDIO).getAbsolutePath());
When I log audioFilename, I get the following path:
/storage/emulated/0/DCIM/folderName/AUD20160425_172620.WAV
I am able to save and play the audio file, but with a caveat:
The file is NOT stored in gallery/myFolder; I can only view the audio file if I run the default play music app and look inside "my playlists".
I have the following permissions in my manifest:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
What am I doing wrong?
I guess we need to tell MediaStore that "a new file is created". Try this:
public void refreshGallery(File file, Context context) {
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
scanIntent.setData(contentUri);
context.sendBroadcast(scanIntent);
}

Categories

Resources