3

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.

Matt Fritze
  • 325
  • 1
  • 5
  • 17
  • What is the error (check logcat)? – petey Jun 06 '13 at 16:55
  • Is the error on startup in your app? or when you do something? if its the latter this is not the correct error. – petey Jun 06 '13 at 17:08
  • Check this: http://stackoverflow.com/questions/10399789/java-lang-runtimeexception-unable-to-instantiate-application-android-app-applic – Sudhee Jun 06 '13 at 17:15
  • Sorry, I ignorantly copied the last error code from logcat that appeared, which was outdated and caused by another issue I'm having with recording audio. The camera issue arises when I launch the camera from a button. The camera opens but when I try to accept the picture I take (by a check mark that the camera app uses automatically) nothing happens. If I click an "x" the camera app closes and I get toasted the message "Error saving image" that I put into the if/else in onActivityResult. No error appears in the logcat though. – Matt Fritze Jun 06 '13 at 17:15

2 Answers2

-1

I was having this same error - resultCode was always 0. Turns out that after I took the picture in the camera app, I was clicking the X on the bottom right instead of the checkmark in the bottom center.

user2616155
  • 330
  • 2
  • 9
-3

it is coming 0 because you have not set result code in the activity , suppose if i call activity b from a .. and on activity b i set setReuslt(reuslt_ok) , then only onactivity result will get the result code as result_ok.. by default the result code is 0

since you are opening the internal camera activity of android , so you are not setting your result code there, so when camera activity finishes it returns the default code back to you

Anshul
  • 106
  • 1
  • 7
  • 1
    How would I set the result code in the camera activity? Thanks – Matt Fritze Jun 06 '13 at 17:59
  • you cant set the resul code in camera activity , so on call back to onactivityresult you have to check it for result code as 0 or by the request code that you have sent – Anshul Jun 06 '13 at 18:02
  • Just to clarify, you're saying that I need to change the if in onActivityResult to : if(resultCode == 0) or if(resquestCode == request_code)? I'm just confused because I thought the image saving process was done before the if/else in onActivityResult and the purpose of the if/else was just to notify the user. Thanks – Matt Fritze Jun 06 '13 at 18:13