谁可以帮我查看这个bug,在我的活动中我使用startActivityForResult(使用相机拍摄图像)。拍照后,我的活动无法恢复。 :(
java.lang.RuntimeException: Unable to resume activity {com.example/com.example.view.main.PersonActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=998, result=-1, data=null} to activity {com.example/com.example.view.main.PersonActivity}: java.lang.NullPointerException
--------- Stack trace ---------
android.app.ActivityThread.performResumeActivity(ActivityThread.java:2458)
android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2486)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2000)
android.app.ActivityThread.access$600(ActivityThread.java:128)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:4514)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
dalvik.system.NativeStart.main(Native Method)
-------------------------------
--------- Cause ---------
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=998, result=-1, data=null} to activity {com.example/com.example.view.main.PersonActivity}: java.lang.NullPointerException
android.app.ActivityThread.deliverResults(ActivityThread.java:2994)
android.app.ActivityThread.performResumeActivity(ActivityThread.java:2445)
android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2486)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2000)
android.app.ActivityThread.access$600(ActivityThread.java:128)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:4514)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
dalvik.system.NativeStart.main(Native Method)
-------------------------------
在我的java代码中,在OnActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String filePath = "";
switch (requestCode) {
case MyActivity.RQ_SALE_TAKE_PHOTO:
{
if (resultCode == RESULT_OK) {
if (data != null && data.getExtras() != null && data.getAction() != null) {
Bitmap tempBitmap = null;
tempBitmap = (Bitmap) data.getExtras().get("data");
//.....
}
} else {
//DO SOMETHING HERE
}
}
break;
}
}
代码呼叫摄像头:
public static File takePhoto(Activity sender, int requestCode) {
System.gc();
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String fileName = Constants.TEMP_IMG + "_" + DateUtils.getCurrentDateTimeWithFormat(null) + ".jpg";
File retFile = new File(ExternalStorage.getTakenPhotoPath(sender), fileName);
if (!retFile.exists())
try {
retFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(retFile));
sender.startActivityForResult(intent, requestCode);
return retFile;
}
任何人都可以帮我解释上述错误,以及如何解决此错误。
答案 0 :(得分:3)
似乎返回意图“data
”为空
确保使用startActivityForResult
和默认的相机应用程序。
默认的Android相机应用程序仅在传回返回的Intent中的缩略图时才返回非空的intent。如果您传递带有要写入的URL的EXTRA_OUTPUT,它将返回一个空意图,并且图片位于您传入的URL中。
答案 1 :(得分:0)
只需做startActivity。当您从其他launchactivity执行startActivityForResult时,您需要将setResult()添加到您的活动中。
现在相机活动没有为您设置任何结果,并且它在onActivityResult函数中崩溃。
答案 2 :(得分:0)
试试这个..
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, requestCode);
答案 3 :(得分:0)
试试这段代码:
try {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(photoPath);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
startActivityForResult(Intent.createChooser(intent, "Capture Image"),
MyActivity.RQ_SALE_TAKE_PHOTO);
} catch (Exception e) {
}