简体   繁体   English

无法使用相机Intent的StartActivityForResult()

[英]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(). 我已经尝试了几乎每个相机意图示例,似乎没有任何工作,并且当它转到startActivityForResult()时我收到一个致命的异常。 It saves the image, and nothing returns null. 它保存图像,没有任何返回null。 I have no idea what the issue is. 我不知道问题是什么。 Code follows 代码如下

MainActivity.java 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 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. 每次尝试记录null时,Android都会抛出NPE(这本身就是一个丑闻)。

The problem is that you believe that the resulting intent contains Uri of the photo taken. 问题是您认为结果意图包含拍摄照片的Uri 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. 这是你的决定,你自己选择了Uri,发送了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. 正如其他一些答案已经说明的那样,它可能也是由于path为空。

Your path can be null beacuse Activty can be destroyed and recreated after starting camera application. 您的path可以为null beacuse可以在启动相机应用程序后销毁并重新创建Activty。 As fdreger said, you can use android.provider.MediaStore.html.EXTRA_OUTPUT extra. 正如fdreger所说,你可以使用android.provider.MediaStore.html.EXTRA_OUTPUT extra。 Here is a snippet: https://gist.github.com/koral--/6173683 这是一个片段: https//gist.github.com/koral--/6173683

It says specifically in logcat that the data you returned is null. 它在logcat中特别指出您返回的数据为null。 In your cameraIntent activity you need to specifically set the results you want to return to your original activity: 在您的cameraIntent活动中,您需要专门设置要返回原始活动的结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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