Bundle extras上的java.lang.NullPointerException = data.getExtras();

时间:2012-12-17 10:13:27

标签: android android-intent nullpointerexception bundle extras

我正在尝试捕获图像并将其保存在图库中。为此在onCreate我的代码

    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
    imageUri = Uri.fromFile(photo);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    startActivityForResult(intent, TAKE_PICTURE);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == TAKE_PICTURE) {
        Log.v("data", "data: " + data);
        Bundle extras = data.getExtras();
        if (extras.containsKey("data")) {
            bitmap = (Bitmap) extras.get("data");
            // ByteArrayOutputStream baos = new ByteArrayOutputStream();
            // bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
            // byte[] image = baos.toByteArray();
            if (bitmap != null) {
                // BitmapFactory.Options options = new
                // BitmapFactory.Options();
                // options.inSampleSize = 5;
                // Bitmap myImage = BitmapFactory.decodeByteArray(image, 0,
                // image.length, options);
                imageView.setImageBitmap(bitmap);
            }
        } else {
            Toast.makeText(getBaseContext(), "Please capture again", Toast.LENGTH_LONG).show();
        }
        showPopUpForUpload();
    }
}

现在我能够捕获图像,但之后我收到了错误

12-17 15:28:56.090: E/AndroidRuntime(4684): FATAL EXCEPTION: main
12-17 15:28:56.090: E/AndroidRuntime(4684): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=null} to activity {com.example.fashiongirl/com.example.fashiongirl.HomeActivity}: java.lang.NullPointerException
12-17 15:28:56.090: E/AndroidRuntime(4684):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2744)
12-17 15:28:56.090: E/AndroidRuntime(4684):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2787)
12-17 15:28:56.090: E/AndroidRuntime(4684):     at android.app.ActivityThread.access$2000(ActivityThread.java:122)
12-17 15:28:56.090: E/AndroidRuntime(4684):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1032)
12-17 15:28:56.090: E/AndroidRuntime(4684):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 15:28:56.090: E/AndroidRuntime(4684):     at android.os.Looper.loop(Looper.java:132)
12-17 15:28:56.090: E/AndroidRuntime(4684):     at android.app.ActivityThread.main(ActivityThread.java:4025)
12-17 15:28:56.090: E/AndroidRuntime(4684):     at java.lang.reflect.Method.invokeNative(Native Method)
12-17 15:28:56.090: E/AndroidRuntime(4684):     at java.lang.reflect.Method.invoke(Method.java:491)
12-17 15:28:56.090: E/AndroidRuntime(4684):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
12-17 15:28:56.090: E/AndroidRuntime(4684):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
12-17 15:28:56.090: E/AndroidRuntime(4684):     at dalvik.system.NativeStart.main(Native Method)
12-17 15:28:56.090: E/AndroidRuntime(4684): Caused by: java.lang.NullPointerException
12-17 15:28:56.090: E/AndroidRuntime(4684):     at com.example.fashiongirl.HomeActivity.onActivityResult(HomeActivity.java:47)
12-17 15:28:56.090: E/AndroidRuntime(4684):     at android.app.Activity.dispatchActivityResult(Activity.java:4541)
12-17 15:28:56.090: E/AndroidRuntime(4684):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2740)
12-17 15:28:56.090: E/AndroidRuntime(4684):     ... 11 more

空指针异常在线

Bundle extras = data.getExtras();

那么问题是什么?

4 个答案:

答案 0 :(得分:6)

我认为这是 Android 4.0 + 的问题。

试着删除,

File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
imageUri = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

当您调用Capture intent时。现在,您的结果包onActivityResult()不是NULL

实际上,当您向Camera Intent提供MediaStore.EXTRA_OUTPUT参数时,相机应用程序不会使用捆绑数据进行回调。 (我在Android 4.0中发现了这个问题)

您必须从Uri获取您设置为MediaStore.EXTRA_OUTPUT参数的Image文件。

<强>更新

或者使用以下pathFromUri()方法并传递您设置为相机意图的Uri并获取真实文件路径,现在为此您不必使用Bundle extras = data.getExtras();

private String pathFromUri(Uri imageUri) {
    String[] filePathColumn = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(imageUri, filePathColumn,
                null, null, null);
    cursor.moveToFirst();
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String filePath = cursor.getString(columnIndex);
    return filePath ;
}

答案 1 :(得分:0)

它为我工作:-----以这种方式尝试

   @Override
  protected void onActivityResult(int requestCode, int resultCode,
        final Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    switch (requestCode) {
    case 1:
        if (resultCode == RESULT_OK) {

                try {
                            Uri selectedImage = intent.getData();
                            bmpSelectedImage = getThumbnail(selectedImage);
                            set_img_camera.setImageBitmap(bmpSelectedImage);

                        } catch (IOException e) {

                            e.printStackTrace();
                        }


        }

    }

答案 2 :(得分:0)

这是我的解决方案:

            Bitmap photo=null;
            File file = getTempFile(this);
            try {
                 photo = Media.getBitmap(getContentResolver(), Uri.fromFile(file) );

                } catch (FileNotFoundException e) {
                  e.printStackTrace();
                } catch (IOException e) {
                  e.printStackTrace();
                }

            if (photo == null) // for newest android version +-4.0
            {
                Uri selectedImage = data.getData();
                photo = Media.getBitmap(this.getContentResolver(),
                        selectedImage);
            }

private File getTempFile(Context context) 
    final File path = new File(Environment.getExternalStorageDirectory(),
            context.getPackageName());
    if (!path.exists()) {
        path.mkdir();
    }
    return new File(path, "image.tmp");
}

答案 3 :(得分:0)

检查'data'是否为null。如果为null,则应跳过代码,否则会导致应用崩溃

if(data != null){    
        Bundle extras = data.getExtras();
                if (extras.containsKey("data")) {
                    bitmap = (Bitmap) extras.get("data");
                    // ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    // bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
                    // byte[] image = baos.toByteArray();
                    if (bitmap != null) {
                        // BitmapFactory.Options options = new
                        // BitmapFactory.Options();
                        // options.inSampleSize = 5;
                        // Bitmap myImage = BitmapFactory.decodeByteArray(image, 0,
                        // image.length, options);
                        imageView.setImageBitmap(bitmap);
                    }
                } else {
                    Toast.makeText(getBaseContext(), "Please capture again", Toast.LENGTH_LONG).show();
                }
                showPopUpForUpload();
    }