在我的onactivityresult上得到空数据

时间:2014-04-05 18:39:06

标签: java android

我有这个代码,我想拍照然后onactivityresult从意图中获取数据并将其转换为位图以显示活动的一些预览但数据为空我不知道为什么。

cameraButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // create Intent to take a picture and return control to the calling application
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);


        }
    });

然后是活动结果:

protected void onActivityResult(int requestCode, int resultCode,
            Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);



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

                data = imageReturnedIntent.getData();
                InputStream stream;



                try {
                    stream = super.getContentResolver().openInputStream(data);

                    bitmap = BitmapFactory.decodeStream(stream);
                    stream.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 

                imageView.setImageBitmap(bitmap);   

    }
    }

1 个答案:

答案 0 :(得分:1)

尝试

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
    }
}

有关详情,请浏览Take a Photo with the Camera