简体   繁体   中英

Unable to StartActivityForResult() with camera Intent

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

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.

Your path can be null beacuse Activty can be destroyed and recreated after starting camera application. As fdreger said, you can use android.provider.MediaStore.html.EXTRA_OUTPUT extra. Here is a snippet: https://gist.github.com/koral--/6173683

It says specifically in logcat that the data you returned is null. In your cameraIntent activity you need to specifically set the results you want to return to your original activity:

 Intent intent = new Intent();
 intent.putExtra("result",yourresult);
 setResult(RESULT_OK,intent);     
 finish();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM