1

这几乎是最后的手段。我一直试图让相机意图无济于事。我的最终目标是将文件路径传递给另一个活动。我已经尝试了几乎所有相机意图示例,但似乎没有任何效果,当它进入 startActivityForResult() 时,我收到了一个致命的异常。它保存图像,并且没有任何内容返回 null。我不知道问题是什么。代码如下

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

日志猫

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
4

3 回答 3

3

每次您尝试记录空值时,Android 都会抛出 NPE(这本身就是一个丑闻)。

问题是您认为生成的意图包含Uri拍摄的照片。它没有

相机应用程序不必告诉您图像的存储位置,因为您已经知道了。这是您的决定,您自己选择了 Uri,发送了 EXTRA_OUTPUT。

更新:如果我误解了问题,请随意忽略我的回答。您可以通过提供行号(至少告诉我们堆栈跟踪中提到了哪些行)并仅发布实际上不起作用的部分代码来防止此类误解。正如其他一些答案已经表明的那样,它也可能是由于为path空。

于 2013-09-27T21:08:07.957 回答
1

path可以是空的,因为 Activty 可以在启动相机应用程序后被销毁和重新创建。正如 fdreger 所说,您可以额外使用android.provider.MediaStore.html.EXTRA_OUTPUT 。这是一个片段:https ://gist.github.com/koral--/6173683

于 2013-09-27T21:19:09.407 回答
0

它在 logcat 中明确表示您返回的数据为空。在您的 cameraIntent 活动中,您需要专门设置要返回到原始活动的结果:

 Intent intent = new Intent();
 intent.putExtra("result",yourresult);
 setResult(RESULT_OK,intent);     
 finish();
于 2013-09-27T21:13:01.553 回答