سؤال

Who can help me review this bug, In my activity I startActivityForResult (used camera to take image). After taken photo, my activity can't resume. :(

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)
-------------------------------

In my java-code, at 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;
    }
}

Code call camera :

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;
}

Can anyone help me to explain the above error, and how to fix this error.

هل كانت مفيدة؟

المحلول

Seems like the return intent "data" is null

Make sure that you use startActivityForResult and the default camera application.

The default Android camera application returns a non-null intent only when passing back a thumbnail in the returned Intent. If you pass EXTRA_OUTPUT with a URL to write to, it will return a null intent and the pictures is in the URL that you passed in.

نصائح أخرى

Just do startActivity. When you do startActivityForResult, from other launchedactivity, you need to setResult() to your activity.

Now camera activity is not setting any result for you and it crashes in onActivityResult function.

try this..

 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, requestCode);

Try this code:

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) {
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top