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);
Related
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?
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.
I currently have an application with a button that takes a photo. However, when I click the button the default camera application opens, then you manually have to take a photo. How do I get the camera application to not open and just take a picture automatically and save it just by pressing the button in my application? I would like to press the button that I created and it takes a photo and saves it automatically.
MainActivity.java
public class MainActivity extends AppCompatActivity {
//for taking photos
static final int REQUEST_IMAGE_CAPTURE = 1;
String currentPhotoPath;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onResume(){
super.onResume();
}
//button to start image capturing process
public void startImageCapture(View view){
dispatchTakePictureIntent();
galleryAddPic();
}
//method for taking a photo
public void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Ensure that there is 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 e){
Log.i("ERROR","Error in trying to create file for image");
}
//continue only if the file was successfully created
if (photoFile != null){
Uri photoURI = FileProvider.getUriForFile(this,"com.example.MyProject.provider",photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
startActivityForResult(takePictureIntent,REQUEST_IMAGE_CAPTURE);
}
}
}
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 galleryAddPic(){
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(currentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
}
For me the easiest solution is using camera view (your own wrapper of Camera2 API or existing CameraView) inside your application and making it invisible or placing some view above it, if you want to hide it.
CameraView API is simple (CameraView Getting Started): just add view into layout, set LifecycleOwner, set callback on taking picture and call camera.takePicture(), when you need to take picture.
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
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.