0

I'm trying to launch a camera activity and then capture the photo when the user saves it and return to the app. This works fine on my phone running Android 2.2 but I get a NullPointerException on my tablet running 4.1.2 or something.

I see the app is getting a NullPointerException in 'onActivityResult' when I try to make a bitmap out of the 'data' that's passed (clearly no data is getting passed):

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    pauseTime = System.currentTimeMillis();
    Log.i(TAG, "Camera result time pauseTime should be reset: " + pauseTime);
    Log.i(TAG, "Camera resultCode is equal to: " + resultCode + " RESULT_OK is: " + RESULT_OK);

    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {

        try {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
        } catch (Exception e) {
            // Data is null getting sent back
            e.printStackTrace();
        }

The error in logcat looks like this:

07-29 17:16:50.795: W/System.err(19517): java.lang.NullPointerException
07-29 17:16:50.795: W/System.err(19517):    at     com.android.thoughtfi.UsersThoughts.onActivityResult(UsersThoughts.java:163)
07-29 17:16:50.795: W/System.err(19517):    at android.app.Activity.dispatchActivityResult(Activity.java:5391)
07-29 17:16:50.795: W/System.err(19517):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3215)
07-29 17:16:50.795: W/System.err(19517):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3262)
07-29 17:16:50.795: W/System.err(19517):    at android.app.ActivityThread.access$1200(ActivityThread.java:140)
07-29 17:16:50.795: W/System.err(19517):    at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
07-29 17:16:50.800: W/System.err(19517):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 17:16:50.800: W/System.err(19517):    at android.os.Looper.loop(Looper.java:137)
07-29 17:16:50.800: W/System.err(19517):    at android.app.ActivityThread.main(ActivityThread.java:4947)
07-29 17:16:50.800: W/System.err(19517):    at java.lang.reflect.Method.invokeNative(Native Method)

Why isn't any data getting return to the application's onactivityresult?

EDIT (for extra info, here is the photoButton):

    photoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // possible option
            String fileName = "temp.jpg";
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, fileName);
            mCapturedImageURI = getContentResolver().insert(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
            startActivityForResult(intent, CAMERA_REQUEST);
        }
    });

EDIT This seems to work ok, but I'm still getting the photo displayed in landscape mode no matter how I take it on the tablet:

onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    pauseTime = System.currentTimeMillis();
    Log.i(TAG, "Camera result time pauseTime should be reset: " + pauseTime);
    Log.i(TAG, "Camera resultCode is equal to: " + resultCode + " ");

    Log.i(TAG, "The intent mCapturedImageURI was: " + mCapturedImageURI);

    Bitmap photo = null;
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        if(data != null){
            photo = (Bitmap) data.getExtras().get("data");
        } else {
                try {
                    photo = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mCapturedImageURI);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }               
        }

        imageView.setImageBitmap(photo);
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(mCapturedImageURI, projection, null,
                null, null);
        int column_index_data = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        capturedImageFilePath = cursor.getString(column_index_data);

Photo BUtton:

photoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // possible option
            String fileName = "temp.jpg";
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, fileName);
            mCapturedImageURI = getContentResolver().insert(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
            startActivityForResult(intent, CAMERA_REQUEST);
        }
    });
4

1 回答 1

0

在相机的参考页面上,文档声明它返回包含捕获图片的文件的路径。

相关代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Toast.makeText(this, "Image saved to: " + data.getData(), Toast.LENGTH_LONG).show();
        } else {
          // whatever
        }
    }

编辑:来自MediaStore.ACTION_IMAGE_CAPTURE 描述

可以发送标准 Intent 操作以使相机应用程序捕获图像并返回它。

调用者可能会传递一个额外的 EXTRA_OUTPUT 来控制该图像将被写入的位置。如果 EXTRA_OUTPUT 不存在,则在额外字段中将小尺寸图像作为位图对象返回。这对于只需要小图像的应用程序很有用。如果存在 EXTRA_OUTPUT,则将全尺寸图像写入 EXTRA_OUTPUT 的 Uri 值。

如果我理解这一点,Camera 活动将返回:

  • 完整图像的 Uri,如果指定了 EXTRA_OUTPUT
  • 如果未指定 EXTRA_OUTPUT,则为getExtras().get("data") 中的缩略图

您是否在启动相机的 Intent 中指定了 EXTRA_OUTPUT 参数?

于 2013-07-29T15:42:17.427 回答