如果您使用whatsapp,然后单击附加按钮,则会弹出一个菜单/弹出窗口,显示您可以选择的应用列表,甚至可以使用照片应用从相机拍摄照片

android - 如何使用一种 Intent 在android中访问照片和相机-LMLPHP

我制作的应用程序具有与访问和拍照的whatapp几乎相同的功能。问题是,我无法使用在弹出对话框中列为应用程序之一的照片应用程序拍照,但是使用whatsapp时可以拍照。当我从应用程序中显示的对话框中单击照片应用程序时,它将显示我所有的图片。但是当我从whatsapp弹出窗口中单击照片应用程序时,它会直接进入相机

public class ImagePickerActivity extends Activity {

private final int SELECT_PHOTO = 1;
private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_picker);

    imageView = (ImageView)findViewById(R.id.imageView);

    Button pickImage = (Button) findViewById(R.id.btn_pick);
    pickImage.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, SELECT_PHOTO);
        }
    });
}

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

    switch(requestCode) {
    case SELECT_PHOTO:
        if(resultCode == RESULT_OK){
            try {
                final Uri imageUri = imageReturnedIntent.getData();
                final InputStream imageStream = getContentResolver().openInputStream(imageUri);
                final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                imageView.setImageBitmap(selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        }
    }
}


在我的Android清单中

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


我需要更改代码中的哪些内容才能从照片应用程序拍照?

最佳答案

尝试添加android.permission.WRITE_EXTERNAL_STORAGE
到你的清单。
相机:

Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);//zero can be replaced with any action code

画廊:
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 1);//one can be replaced with any action code

onactivity结果代码:
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    switch(requestCode) {
    case 1:
        if(resultCode == RESULT_OK){
            Uri selectedImage = imageReturnedIntent.getData();
            imageview.setImageURI(selectedImage);
        }
    break;
    case 0:
        if(resultCode == RESULT_OK){
            Uri selectedImage = imageReturnedIntent.getData();
            imageview.setImageURI(selectedImage);
        }
    break;
    }

}

10-07 19:28
查看更多