vendredi 27 février 2015

Camera crashes after taking picture and hitting ok



I am developing an app which sends an intent to take a picture. I am using a Galaxy S5 but this problem is confirmed on 3 different devices.


First the camera started doing a loop where hitting ok just returned back to take another photo. I refactored the code and fixed that problem but had others. Now, after bringing up the take photo screen, using the back button goes back successfully and hitting retry after taking the photo works fine. However hitting ok does one of three things:



  • Works (~%20)

  • throws an error outside my own error handler. "Unfortuneatly MyApp has stopped unexpectedly". Then it goes back to the calling activity, throwing an error from my handler because now it is trying to read from something which is now null (presumably because the app crashed). Breakpoints are now ignored and I have to launch debug again. I can go back to first page of app and go through process again, but debug doesn't respond. (~30%)

  • Same as before except my error is thrown, I click the back button to get rid of it then the back button to go back a screen and then I get the "Unfortuneatly myApp has stopped". (~50%)


I have not been able to find the error being thrown from the camera which causes the initial wierdness and then causes everything to crash. I have a breakpoint on onActivityResult which is never trigged if an error is thrown. Also my catch block on startActivyForResult never throws that error. The errors always come from NPE's in the onCreate as the activity tries to start up again after the photo is taken or the app throwing the "Unfortuneatly myApp has stopped".


I have a ton more detail but I don't want to overload with info. Basically when I hit ok it either works ~20% of the time or all hell breaks loose the other 80% of the time. Can anyone point me in the right direction as to why the results are so seemingly random and unpredictable? Thanks!


I have been following this tutorial: http://ift.tt/1cCQgTc


callingActivty



cameraAN = new CameraActivityNative(handler,SetCounterActivity.this);
Intent intent = cameraAN.getPictureIntent();
startActivityForResult(intent,cameraAN.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == cameraAN.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Utils.log("Set Counter Activity", "Photo saved to phone", 'd');
try {
File f = new File(cameraAN.getFileUri().getPath());
//Allow user option to upload to traffic server
cameraAN.launchUploadPrompt(f);
}
catch (Exception e){
handler.error(this, e);
}

} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
handler.toast("Error saving the image, please try again.");
}
}

}


CameraActivityNative.java



public Intent getPictureIntent() {
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// create a file to save the image
try {
fileUri = createMultimediaFile(MEDIA_TYPE_IMAGE, handler);
}
catch (Exception e){
Utils.log("Camera Activity Native", "cannot create file", 'e');
}
// set the image file name
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

// start the image Intent
return intent;
}
private Uri createMultimediaFile(int type, FieldApplicationManager.FAMEventHandler handler) throws UnsupportedOperationException, IOException{
// 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), "MyDir");

// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Utils.log("CameraActivityNative", "failed to create directory", 'd');
return null;
}
}

// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName;

String fileSuffix;
if (type == MEDIA_TYPE_IMAGE){
imageFileName = "IMG_"+ timeStamp + ".jpg";
}
else if (type == MEDIA_TYPE_VIDEO) {
imageFileName = "VID_"+ timeStamp + ".mp4";
}
else {
throw new UnsupportedOperationException("Media type not supported");
}

File mediaFile = new File(mediaStorageDir, imageFileName);

// Save a file: path for use with ACTION_VIEW intents
// galleryAddPic("file:" + mediaFile.getAbsolutePath());
return Uri.fromFile(mediaFile);


}


Logcat error. I'm not sure this is actually releveant as this is the error that is thrown after the camera has already crashed.


02-27 17:56:27.609 17153-17153/com.geocounts.android.fieldteam E/ERROR﹕ Message: Attempt to read from field 'java.lang.String com.geocounts.android.api.CountSurvey.stationid' on a null object reference Last System Info: null Parameter: onGCFCreate Stack Trace: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.geocounts.android.api.CountSurvey.stationid' on a null object reference at com.geocounts.android.gui.SetCounterActivity.onGCFCreate(SetCounterActivity.java:33) at com.geocounts.android.gui.GeocountsFieldAppActivity.onCreate(GeocountsFieldAppActivity.java:64) at android.app.Activity.performCreate(Activity.java:6221) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2728) at android.app.ActivityThread.access$900(ActivityThread.java:172) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5837) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)




Aucun commentaire:

Enregistrer un commentaire