1
Good afternoon everyone, I started an internship a couple of months ago and both this community and the foreign Stack have been my best friends on this new journey.
Currently I am developing an app by Android IDE Studio 2.1.2 and have come across some problems using the camera’s native API in the project. I need to take a photo with the camera, save this photo in some folder, cache preferably, and not make this photo available on the media or for any other application, only mine.
I’ve used the Intent straight from the camera and every solution I seek I catch 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, he’s safe in the mobile gallery. I try to create a cache folder to save the photo, but it gives error when creating the directory. And if you create the directory, save in a folder to the photos of the APP and also shows in the media.
I don’t know if I have to use the programmatic mode to use the camera or if what I want I can solve with the native Intent of the camera.
EDIT: I’ll put in the last code 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 commented on 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 photo preview in a thumbnail.
If you pass one path does not need the date, the photo is recorded on path. Without knowing what code you’re using It’s hard to know what’s wrong.
– ramaral
Note that in order to use the lines you have commented on you have to add the permission WRITE_EX TERNAL_STORAGE in the Androidmanifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

– ramaral
are already added
– Kauã Estriga
In one of the searches I did I saw that to use the file saved only in my APP, you have to save them in the internal memory. So that way that I maintain control, secrecy and when the APP is deleted, the photos will also be. If you still have it in the folder, because I want to use a Cache folder.
– Kauã Estriga
The camera application cannot write to the internal memory of your application, it has no permissions.
– ramaral
In some researches I saw that to save in internal memory the APP does not need permissions. As I am opening the camera by APP then it could not record the image in internal memory?
– Kauã Estriga
This is true for your application. Although it is "called" by its application the camera is another application.
– ramaral
Then I need to use the fully programmed camera. Open the camera, open preview screen, preview and tals? Does this resolve?
– Kauã Estriga
Yes but it’s a bit complicated. If you want the photos to be deleted by uninstalling the app and not appear "in the media"
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
 Environment.DIRECTORY_PICTURES), "MyCameraApp");
useFile mediaStorageDir = new File(getExternalFilesDir(), "MyCameraApp");
– ramaral