简体   繁体   English

在我的活动结果上获取空数据

[英]getting null data on my activityresult

i have this code, i want to take a picture and then onactivityresult take the data from the intent and make transformr it to a bitmap to display a little preview on the activity but data is null i dont know why. 我有这段代码,我想拍照,然后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);


        }
    });

then on the activity result: 然后在活动结果上:

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

    }
    }

try like 尝试像

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

For more details go through Take a Photo with the Camera 有关更多详细信息,请通过相机拍摄照片。

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

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