我正在开发一个发送拍照 Intent 的应用程序。我使用的是 Galaxy S5,但此问题已在 3 种不同的设备上得到确认。
首先,相机开始循环,点击确定后返回拍摄另一张照片。我重构了代码并解决了那个问题,但还有其他问题。现在,在调出拍照屏幕后,使用后退按钮成功返回并在拍照后点击重试工作正常。然而,点击 OK 会做以下三件事之一:
- 作品(~%20)
- 在我自己的错误处理程序之外 抛出一个错误。 “很遗憾,MyApp 意外停止”。然后它返回到调用 Activity ,从我的处理程序中抛出一个错误,因为现在它正在尝试从现在为 null 的内容中读取(大概是因为应用程序崩溃了)。断点现在被忽略,我必须再次启动调试。我可以返回到应用程序的第一页并再次执行该过程,但调试没有响应。 (~30%)
- 除了抛出我的错误外,与之前相同,我点击后退按钮摆脱它,然后点击后退按钮返回屏幕,然后我得到“不幸的是我的应用程序已停止”。 (~50%)
我没有能够找到从相机抛出的错误,它会导致最初的怪异,然后导致一切崩溃。我在 onActivityResult 上有一个断点,如果抛出错误,它永远不会被触发。此外,我在 startActivityForResult
上的 catch block 从不抛出该错误。错误总是来自 onCreate 中的 NPE,因为在拍摄照片后 Activity 试图再次启动或应用程序抛出“不幸的是我的应用程序已停止”。
我有很多详细信息,但我不想让信息过多。基本上,当我点击 OK 时,它要么在大约 20% 的时间内工作,要么在其他 80% 的时间内完全崩溃。谁能指出我正确的方向,为什么结果看起来如此随机和不可预测?谢谢!
我一直在关注这个教程: http://developer.android.com/training/camera/photobasics.html
调用 Activity
private class SetCounterActivityMenu implements OnMenuItemClickListener {
private FieldAppActivity a;
private SetCounterActivityMenu(FieldAppActivity _a) {
this.a = _a;
}
@Override
public boolean onMenuItemClick(MenuItem view) {
switch (view.getItemId()) {
case 777771: {
try{
//nextActivity(CameraActivityNative.class);
cameraAN = new CameraActivityNative(handler,SetCounterActivity.this);
Intent intent = cameraAN.getPictureIntent();
startActivityForResult(intent, cameraAN.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
catch (Exception ex){
handler.error("Error occured taking picture", ex);
}
break;
}
case 777772: {
a.showMap(MapShowsWhat.survey);
break;
}
}
return false;
}
}
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);
}
日志错误。我不确定这是否真的相关,因为这是相机已经崩溃后引发的错误。
02-27 17:56:27.609 17153-17153/android.fieldteam E/ERROR﹕ Message: Attempt to read from field 'java.lang.String 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 android.api.CountSurvey.stationid' on a null object reference
at android.gui.SetCounterActivity.onGCFCreate(SetCounterActivity.java:33)
at android.gui.FieldAppActivity.onCreate(FieldAppActivity.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)
最佳答案
我很抱歉没有更清楚地问这个问题,因为我自己也不确定。结果一次出现了多个问题,我错误地认为它们都是同一个问题。希望这会在将来帮助其他人。这是我遇到的问题的总结
每当它从相机 Intent 返回时,调试器就是
分离(我认为这是因为相机的错误
但结果有点正常)- 修复:强制调试器等待并重新连接
Debugger disconnects when calling external intent (camera)
- 修复:强制调试器等待并重新连接
当从相机 Intent 返回时,应用程序有时会被操作系统破坏(我假设内存不足
原因)。这导致了零星的行为和我的状态对象
返回时会有几个空字段,使我的应用程序崩溃。- 修复:在发送图片之前将状态对象写入文件,加载我处理 onCreate 的父 Activity 类,然后转到 子类 Activity 。
当应用被销毁时,先调用 onCreate 然后再调用 onActivityResult,而不是在应用未被销毁时才调用 onActivityResult。当我在创建时调用相机 Intent 时,这导致了奇怪的行为。
- 修复:检查 savedInstanceState 是否为空,以检查 Activity 是否有以前的数据,如果没有,它会启动相机。这解决了有时会陷入拍照和返回相机的无限循环的问题。
由于其他问题,错误并不总是出现在我的正常错误 toast 或我的日志中(有时与调试器分离),这使得调试变得更加困难,因为它不一致。
- 与#1 相同。
关于java - 拍照并点击确定后相机崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28776445/