How to capture photo with existing camera app in android? - java

I need to take photo using existing camera app and store it. I took this code from here
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}
private void dispatchTakePictureIntent() {
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 = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
...
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
The problem with this code is
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
is depreciated
Once i enter the camera app a file is created even if the picture is not taken
What changes should i make so that a file will be created only when an image is captured in the camera app?

Related

How to get my saved photos in this file manager in Android Studio

I have some issues with my application, I have code that opening camera and saving taked photos but, i can't locate them when i'm using file manager. Can someone help me?
This is, my code to take photos:
cameraBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mPhotoUri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider", createImageFile());
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.setClipData(ClipData.newRawUri("A photo", mPhotoUri));
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoUri);
startActivityForResult(intent, ACTIVITY_REQUEST_CODE);
}
});
private File createImageFile() {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = new File(getFilesDir(), "images/");
if (!storageDir.exists()) storageDir.mkdirs();
File image = null;
try {
image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
} catch (IOException e) {
e.printStackTrace();
}
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
This staff saves photos in: /data/data/com.example.miarkait/files/images
And this is code that opening file manager:
imageBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_GET_CONTENT, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
final int ACTIVITY_SELECT_IMAGE = 1234;
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
}
});
But in this manager, i can see only photos taken by "original" camera:
RESULT
File(getFilesDir()....
getFilesDir() is your app private storage location.
No other app has access.
Not even file managers.
And you cannot pick it with ACTION_GET_CONTENT.

Open Camera Button is saved in the final png screenshot

I created an app to take photos using mobile's camera, show GPS coordinates as a text, and save the image to mobile's gallery. The problem is that the final screenshot saved in mobile's picture gallery contains the "Open Camera" BUTTON. How can I remove it from the screenshot (png) saved?
This is my code:
/* capture image */
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = null;
try {
photoFile = createImageFile();
}
catch (IOException e) {
e.printStackTrace();
}
//fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
String authorities = getApplicationContext().getPackageName() + ".provider";
Uri imageUri = FileProvider.getUriForFile(this, authorities, photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
/* create image file to store photo */
String mCurrentPhotoPath;
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
); // Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
Where to add the openCameraButton.setVisibility(View.GONE); and openCameraButton.setVisibility(View.VISIBLE); ? Is there any way to NOT store my image to the mobile's default pictures directory after taking photo ?
Based on the description and the photo you've attached, we can assume that this button opens the phone's camera and (probably) takes a picture. If that's true, you can hide the visibility of the button before the screenshot is taken with openCameraButton.setVisibility(View.GONE); and show the button again, once you've taken the screenshot: openCameraButton.setVisibility(View.VISIBLE);

File does not exist in android?

I have an app which captures images using the camera like this.
String[] galleryPermissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(this, galleryPermissions)){
//Camera
if (index == 0) {
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 = createImageFile();
} catch (IOException ex) {
Toast.makeText(getApplicationContext(),ex.getMessage(),Toast.LENGTH_LONG).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = null;
// N is for Nougat Api 24 Android 7
if (Build.VERSION_CODES.N <= android.os.Build.VERSION.SDK_INT) {
// FileProvider required for Android 7. Sending a file URI throws exception.
photoURI = FileProvider.getUriForFile(this,
BuildConfig.APPLICATION_ID + ".provider",
photoFile);
Log.v("highbuild",photoURI.toString());
} else {
// For older devices:
// Samsung Galaxy Tab 7" 2 (Samsung GT-P3113 Android 4.2.2, API 17)
// Samsung S3
photoURI = Uri.fromFile(photoFile);
Log.v("lowbuild",photoURI.toString());
}
imageuriduplicate = photoURI;
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, 0);
}
else{
Log.v("file", "photo file is null");
Toast.makeText(getApplicationContext(),"Photo file is null", Toast.LENGTH_LONG).show();
}
}
}
//Gallery
else if (index == 1) {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.INTERNAL_CONTENT_URI);
pickPhoto.setType("image/*");
startActivityForResult(pickPhoto, 1);//one can be replaced with any action code
}
}
else {
EasyPermissions.requestPermissions(this, "Access for storage",
101, galleryPermissions);
}
My create file image method is like this
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "AndroidUpload" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
if(!storageDir.exists()){
boolean s = new File(storageDir.getPath()).mkdirs();
if(!s){
Log.v("not", "not created");
}
else{
Log.v("cr","directory created");
}
}
else{
Log.v("directory", "directory exists");
}
File image = File.createTempFile(
imageFileName, /* prefix */
".png", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
I then later save the path to an arraylist and access it.
But when i try to access the file like this
Uri imageuri = Uri.parse(receipts.get(i).imageURI);
File file = new File(imageuri.getPath());
if(file.exists()){
Log.v("exists","file exists");
}
else{
Log.v("no","file doesnt exist");
}
I get the file doesnt exist error.
I have given all the necessary permissions and I am still getting this error. How can fix this?
Make sure the storageDir exists. If not create directories using
new File(path_to_dir).mkdirs();

getExternalStoragePublicDirectory not working, while getExternalFilesDir is

I've managed to finally make the code from Android Developer website work.
The original working code is this:
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
private void dispatchTakePictureIntent() {
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 = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.test.testapp.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
It's working, this app launches the camera and takes pictures, but when I changed the part "getExternalFilesDir" to "getExternalStoragePublicDirectory", the button doesn't launch the camera app

Saving camera pictures in emulated genymotion not working

I am not able to save pictures in sdcard using Genymotion emulator.
Code for creating the file for the image
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
//File storageDir = Environment.getExternalStoragePublicDirectory(
//Environment.DIRECTORY_PICTURES);
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
// Save a file: path for use with ACTION_VIEW intents
Toast.makeText(AddPhotoActivity.this, mCurrentPhotoPath + "file", Toast.LENGTH_SHORT).show();
return image;
}
The above function is not returning anything. Below function is for Initiating camera intent and click picture.
private void dispatchTakePictureIntent() {
// Toast.makeText(AddPhotoActivity.this, "Hello", Toast.LENGTH_SHORT).show();
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 = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
The problem is something in File.createTempFile() function I guess.

Categories