Good afternoon everyone, I started a training course about two months ago and both this community and the foreign Stack have been my best friends on this new journey.
Currently I'm developing an application through the IDE Android Studio 2.1.2 and I came across some issues when using the native camera API in the project. I need to take a picture with the camera, save this photo in some folder, preferably cache, and do not make this photo available in the media or any other application, just mine.
I've already used the Direct Intent of the camera and every solution I'm looking for I lock at some point.
If I choose a Path to save the photo, the date of onResultActivity
comes as Null.
If I save without passing the path, it gets saved in the cell gallery. I try to create a cache folder to save the photo, but it gives an error when creating the directory. And if you create the directory, saved in a folder for the APP photos and also shows in the media.
I do not know if I have to use the programmatic mode to use the camera or if what I want I can resolve with the camera's native Intent.
EDIT: I will put the last code that I executed for better understanding.
private void abrirCamera(){
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
//intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAM_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAM_REQUEST) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
//Toast.makeText(this, "Image saved to:\n" +
// data.getData(), Toast.LENGTH_LONG).show();
Bundle extras = data.getExtras();
bitmap = (Bitmap) extras.get("data");
img.setImageBitmap(bitmap);
AtualizaCor task = new AtualizaCor();
task.execute(new String[] { "http://www.vogella.com/index.html" });
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
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 {
return null;
}
return mediaFile;
}
I've commented these two lines here:
//fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
//intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
Because they were bringing the date as Null. And I need it to leave a preview of the photo in a thumbnail.