Android onclick单选按钮相机意图不起作用

时间:2015-09-24 10:39:08

标签: android onactivityresult

我有一个单选按钮。当我点击单选按钮时,使用相机拍摄图像后打开相机意图。图像未更新到图像视图。

我使用了清单文件中的所有权限。

RB_PhotoStatus
            .setOnCheckedChangeListener(new android.widget.RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group,
                        int checkedId) {
                    switch (checkedId) {
                    case R.id.yes:
                        //photoCollected = "Yes";
                         // create intent with ACTION_IMAGE_CAPTURE action 
                        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(intent, 0);

                        break;

                    case R.id.no:
                        photoCollected = "No";
                        break;
                    }

                }
            });




@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {



          super.onActivityResult(requestCode, resultCode, data);

            Bitmap bp = (Bitmap) data.getExtras().get("data");
            imageView1.setImageBitmap(bp);


    }

4 个答案:

答案 0 :(得分:2)

简单拍摄照片!

此答案解释了如何使用现有的相机应用程序拍摄照片。

<manifest ... >
    <uses-feature android:name="android.hardware.camera"
                  android:required="true" />
    ...
</manifest>

请求相机应用。

static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

带有活动覆盖功能的数据的相机返回意图onActivityResult如下: -

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

有关详细信息,请参阅以下链接:

http://developer.android.com/training/camera/photobasics.html

答案 1 :(得分:1)

它不起作用,因为Camera Intent不会返回整个BitMap,而只返回创建文件的引用(Uri)。

 Uri selectedImage = data.getData();

您可以使用BitmapFactory.decodeFile

从此Uri重新加载BitMap

答案 2 :(得分:1)

在radiobuttonclick();

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(cameraIntent, CAMERA_REQUEST); 

onActivityResult();

Uri originalUri = data.getData();
imageview.setImageURI(originalUri);

如果你想得到位图,那么

bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), originalUri);

答案 3 :(得分:0)

private String mCurrentPhotoPath;
final String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(state)) {

            final File image = create_directory();
            // Save a file: path for use with ACTION_VIEW intents
            try {
                mCurrentPhotoPath = image.getAbsolutePath();
            } catch (Exception e) {
                e.printStackTrace();
            }

            final Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                if (image != null) {
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
                    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
                } else {
                    snackbar = Snackbar.make(findViewById(android.R.id.content), "An error has occurred", Snackbar.LENGTH_SHORT);
                    snackbar.setAction("Dismiss", clickListener);
                    snackbar.show();
                }
            }
        }

public File create_directory() {

        // Create an image file name
        final String imageFileName;
        final String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date());
        final String user_id = "1_";
        imageFileName = user_id + timeStamp + "_";

        final String proj_name = "test";
        final String folder_timeStamp = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH).format(new Date());
        final String path = "/TEST/" + proj_name + "/" + folder_timeStamp;
        final File dr = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), path);
        if (!dr.exists()) {
            dr.mkdirs();
        }
        File image = null;
        try {
            image = File.createTempFile(imageFileName, ".jpg", dr);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE) {
        if (resultCode == RESULT_OK){
         //do something with the image which is stored in mCurrentPhotoPath
        }
    } 
}