I am trying to include a camera in my app that saves the files locally on the SD card. The camera application starts, but the resultCode is always 0. I have added the following permissions to my Manifest:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Here is the code for my the camera:
@SuppressLint("SimpleDateFormat")
private void takePicture(){
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/resources/resources/WI1");
SimpleDateFormat timeStampFormat = new SimpleDateFormat("MM/dd/yyyy");
String image_name =username +"-"+ timeStampFormat.format(new Date())+".png";
File image = new File(imagesFolder, image_name);
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
int request_code = 100;
startActivityForResult(imageIntent, request_code);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
Toast.makeText(this, "Image Saved", Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(this,"Error Saving Image, please throw device at wall", Toast.LENGTH_SHORT).show();
} // end on activity result
What's causing the bug? Thanks!
EDIT: I removed the previously posted logcat information, as it was not relevant to this issue. EDIT2:
I half solved the issue, if I use this code the camera works just fine. Could someone tell me what would cause that?
private void takePicture(){
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/resources/resources/WI1");
String image_name = "matt"+image_count+".png";
image_count+=1; // this is at the moment useless.
File image = new File(imagesFolder, image_name);
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
int request_code = 100;
startActivityForResult(imageIntent, request_code);
}
EDIT 3: The issue is with the timeStampFormat, if I exclude it the camera works just fine. Could someone explain why? If I'm not mistaken, it's because the date format I chose has forward slashes in it.