Unable to StartActivityForResult() with camera Intent

cdurth

this is pretty much a last resort. I have been trying to get a camera intent working to no avail. My end goal is to pass the filepath to another activity. I have tried pretty much every camera intent example out there and nothing appears to be working, and I receive a fatal exception when it goes to the startActivityForResult(). It saves the image, and nothing returns null. I have no idea what the issue is. Code follows

MainActivity.java

package com.example.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;



import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private static final int SELECT_IMAGE = 1;
    private static final int CAMERA_REQUEST = 1337;
    private Uri imageUri;
    private ImageView mImageView;
    private Button mButton;
    private String path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = (ImageView) findViewById(R.id.imageView1);
        mButton = (Button) findViewById(R.id.button1);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // create an intent to invoke a image capture device
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
                File output = new File(dir,"test.jpg");
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(output));
                path = output.getAbsolutePath();
                Log.e("TEST", cameraIntent == null ? "true" : "false");
                Log.e("TEST", dir == null ? "true" : "false");
                Log.e("TEST", output == null ? "true" : "false");
                Log.e("TEST", path == null ? "true" : "false");
                System.out.println(CAMERA_REQUEST);

                // start the camera activity
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });
    }

    public void onSelectAnImage(View v) {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        Uri data = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath());
        photoPickerIntent.setDataAndType(data, "image/*");
        startActivityForResult(photoPickerIntent, SELECT_IMAGE);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
            case SELECT_IMAGE:

                // get the URI of the image that the user selected.
                Uri picturePath = data.getData();           
                System.out.println(picturePath);        
                Intent intent = new Intent(this, ImageInformationActivity.class);
                intent.putExtra("IMAGE_FILENAME", picturePath.toString());

                //intent.putExtra("uri", pass);
                // start the intent.
                startActivity(intent);

            case CAMERA_REQUEST:
                System.out.println(path);
            }
        }
    }
}

LogCat

09-27 16:56:05.043: I/Adreno200-EGLSUB(31741): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-27 16:56:09.558: I/System.out(31741): content://media/external/images/media/1205
09-27 16:56:09.618: I/System.out(31741): /storage/sdcard0/DCIM/test.jpg
09-27 16:56:09.678: I/Adreno200-EGLSUB(31741): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-27 16:56:09.718: I/System.out(31741): android.widget.ImageView@41ef7f60
09-27 16:56:09.728: I/System.out(31741): android.os.ParcelFileDescriptor$AutoCloseInputStream@41ef9488
09-27 16:56:09.758: D/dalvikvm(31741): GC_FOR_ALLOC freed 73K, 42% free 12365K/21315K, paused 22ms, total 22ms
09-27 16:56:09.868: D/dalvikvm(31741): GC_CONCURRENT freed 3K, 19% free 40105K/49095K, paused 2ms+2ms, total 21ms
09-27 16:56:10.349: I/System.out(31741): android.graphics.Bitmap@41eed2a0
09-27 16:56:10.349: I/Choreographer(31741): Skipped 39 frames!  The application may be doing too much work on its main thread.
09-27 16:56:10.379: I/Adreno200-EGLSUB(31741): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-27 16:56:12.521: I/Adreno200-EGLSUB(31741): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-27 16:56:12.571: E/SpannableStringBuilder(31741): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
09-27 16:56:12.571: E/SpannableStringBuilder(31741): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
09-27 16:56:13.522: E/TEST(31741): false
09-27 16:56:13.522: E/TEST(31741): false
09-27 16:56:13.522: E/TEST(31741): false
09-27 16:56:13.522: E/TEST(31741): false
09-27 16:56:13.522: I/System.out(31741): 1337
09-27 16:56:14.603: W/IInputConnectionWrapper(31741): showStatusIcon on inactive InputConnection
09-27 16:56:20.840: W/dalvikvm(31741): threadid=1: thread exiting with uncaught exception (group=0x411ef438)
09-27 16:56:20.850: E/AndroidRuntime(31741): FATAL EXCEPTION: main
09-27 16:56:20.850: E/AndroidRuntime(31741): java.lang.RuntimeException: Unable to resume activity {com.example.test/com.example.test.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1337, result=-1, data=null} to activity {com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException
09-27 16:56:20.850: E/AndroidRuntime(31741):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2639)
09-27 16:56:20.850: E/AndroidRuntime(31741):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2667)
09-27 16:56:20.850: E/AndroidRuntime(31741):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2140)
09-27 16:56:20.850: E/AndroidRuntime(31741):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3576)
09-27 16:56:20.850: E/AndroidRuntime(31741):    at android.app.ActivityThread.access$800(ActivityThread.java:143)
09-27 16:56:20.850: E/AndroidRuntime(31741):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
09-27 16:56:20.850: E/AndroidRuntime(31741):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-27 16:56:20.850: E/AndroidRuntime(31741):    at android.os.Looper.loop(Looper.java:137)
09-27 16:56:20.850: E/AndroidRuntime(31741):    at android.app.ActivityThread.main(ActivityThread.java:4950)
09-27 16:56:20.850: E/AndroidRuntime(31741):    at java.lang.reflect.Method.invokeNative(Native Method)
09-27 16:56:20.850: E/AndroidRuntime(31741):    at java.lang.reflect.Method.invoke(Method.java:511)
09-27 16:56:20.850: E/AndroidRuntime(31741):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
09-27 16:56:20.850: E/AndroidRuntime(31741):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
09-27 16:56:20.850: E/AndroidRuntime(31741):    at dalvik.system.NativeStart.main(Native Method)
09-27 16:56:20.850: E/AndroidRuntime(31741): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1337, result=-1, data=null} to activity {com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException
09-27 16:56:20.850: E/AndroidRuntime(31741):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3205)
09-27 16:56:20.850: E/AndroidRuntime(31741):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2626)
09-27 16:56:20.850: E/AndroidRuntime(31741):    ... 13 more
09-27 16:56:20.850: E/AndroidRuntime(31741): Caused by: java.lang.NullPointerException
09-27 16:56:20.850: E/AndroidRuntime(31741):    at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
09-27 16:56:20.850: E/AndroidRuntime(31741):    at com.example.test.MainActivity.onActivityResult(MainActivity.java:86)
09-27 16:56:20.850: E/AndroidRuntime(31741):    at android.app.Activity.dispatchActivityResult(Activity.java:5363)
09-27 16:56:20.850: E/AndroidRuntime(31741):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3201)
09-27 16:56:20.850: E/AndroidRuntime(31741):    ... 14 more
fdreger

Android throws NPE (which is a scandal in itself) every time you try to log a null.

The problem is that you believe that the resulting intent contains Uri of the photo taken. It does not.

Camera app does not have to tell you where the image is stored, because you already know. It was your decision and you have chosen the Uri yourself, sending the EXTRA_OUTPUT.

update: Feel free to ignore my answer if I misunderstood the problem. You can prevent such misunderstandings by providing line numbers (at least tell us which lines are mentioned in the stacktrace) and only publishing parts of the code that actually don't work. As some other answer already tells, it might as well be due to path being null.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unable to StartActivityForResult() with camera Intent

From Dev

Unable to start service with intent. Fail to connect to camera service

From Dev

How to use intent startactivityforresult method?

From Dev

startActivityForResult: No Activity found to handle Intent

From Dev

Camera Intent in Fragment

From Dev

Camera Intent call onDestroy()

From Dev

Camera Intent destroys Activity

From Dev

fileNotFoundException camera intent - Android

From Dev

Unable to resume activity on onActivityResult when ACTION_IMAGE_CAPTURE intent returns on landscape camera orientation

From Dev

Camera intent doesn't show in intent chooser

From Dev

Intent does not set the camera parameters

From Dev

Camera Intent returns null onActivityResult

From Dev

Camera Intent not returning to calling Activity

From Dev

Using android camera with an explicit intent

From Dev

Camera Intent vs Activity/Fragment

From Dev

Android camera and photo picker intent

From Dev

Camera Intent not returning to calling Activity

From Dev

Sent camera picture via intent

From Dev

Error on camera image capture with intent

From Dev

Camera Intent returns null onActivityResult

From Dev

Using android camera with an explicit intent

From Dev

onActivityResult() not called after startActivityForResult() with Intent.ACTION_GET_CONTENT

From Dev

StartActivityForResult - check for existence of intent extra or use custom result code?

From Dev

Intent lost on BACK button even if I try startActivityForResult, saveInstanceState in FragmentPagerAdapter

From Dev

In Android Transfuse, how to pass Intent back with startActivityForResult and onActivityResult

From Dev

StartActivityForResult - check for existence of intent extra or use custom result code?

From Dev

Intent lost on BACK button even if I try startActivityForResult, saveInstanceState in FragmentPagerAdapter

From Dev

startActivityForResult(intent, CROP_FROM_ALBUM) returns 0 on 'onResultActivity' method

From Dev

Android startActivityForResult returns null to onActivityResult when using camera

Related Related

  1. 1

    Unable to StartActivityForResult() with camera Intent

  2. 2

    Unable to start service with intent. Fail to connect to camera service

  3. 3

    How to use intent startactivityforresult method?

  4. 4

    startActivityForResult: No Activity found to handle Intent

  5. 5

    Camera Intent in Fragment

  6. 6

    Camera Intent call onDestroy()

  7. 7

    Camera Intent destroys Activity

  8. 8

    fileNotFoundException camera intent - Android

  9. 9

    Unable to resume activity on onActivityResult when ACTION_IMAGE_CAPTURE intent returns on landscape camera orientation

  10. 10

    Camera intent doesn't show in intent chooser

  11. 11

    Intent does not set the camera parameters

  12. 12

    Camera Intent returns null onActivityResult

  13. 13

    Camera Intent not returning to calling Activity

  14. 14

    Using android camera with an explicit intent

  15. 15

    Camera Intent vs Activity/Fragment

  16. 16

    Android camera and photo picker intent

  17. 17

    Camera Intent not returning to calling Activity

  18. 18

    Sent camera picture via intent

  19. 19

    Error on camera image capture with intent

  20. 20

    Camera Intent returns null onActivityResult

  21. 21

    Using android camera with an explicit intent

  22. 22

    onActivityResult() not called after startActivityForResult() with Intent.ACTION_GET_CONTENT

  23. 23

    StartActivityForResult - check for existence of intent extra or use custom result code?

  24. 24

    Intent lost on BACK button even if I try startActivityForResult, saveInstanceState in FragmentPagerAdapter

  25. 25

    In Android Transfuse, how to pass Intent back with startActivityForResult and onActivityResult

  26. 26

    StartActivityForResult - check for existence of intent extra or use custom result code?

  27. 27

    Intent lost on BACK button even if I try startActivityForResult, saveInstanceState in FragmentPagerAdapter

  28. 28

    startActivityForResult(intent, CROP_FROM_ALBUM) returns 0 on 'onResultActivity' method

  29. 29

    Android startActivityForResult returns null to onActivityResult when using camera

HotTag

Archive