1

我正在尝试拍照并在 ImageView 中显示。如果我尝试从图库中填充 ImageView,它可以完美地工作,但如果我使用相机拍照,它就不起作用。

这是我正在做的事情:

//选择从相机或画廊拍摄

public class ProfileFragment extends Fragment    
.....
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Escolha a Foto");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) { // pick from
                                                                // camera
            if (item == 0) {

                Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(takePicture, 0);//zero can be replced with any action code
            } else {
                // pick from file
                Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                           android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(pickPhoto , 1);//one can be replced with any action code
            }
        }
    });
....
}

@Override
public void onActivityResult(int requestCode, int resultCode,
        Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    switch (requestCode) {
    case 0:
        if (resultCode == -1) {
            Uri selectedImage = imageReturnedIntent.getData();
            imageView.setImageURI(selectedImage);
        }

        break;
    case 1:
        if (resultCode == -1) {
            Uri selectedImage = imageReturnedIntent.getData();
            imageView.setImageURI(selectedImage);
        }
        break;
    }
}

private void captureImageInitialization() {

    final String[] items = new String[] { "New Photo",
            "Choose from Gallery" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.select_dialog_item, items);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setTitle("Choose Photo");
    builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            if (item == 0) {
                Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(takePicture, 0);//zero can be replced with any action code
            } else {

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

    dialog = builder.create();
}


知道有什么问题吗?

更新:保存照片后调用片段中的方法“onCreateView()”...这就是 ImageView 为空的原因...我该如何解决?

UPDATE2:我使用相机时,mainActivity 中的 onCreate() 一直被调用。

提前致谢。

4

2 回答 2

0

像这样试试

始终使用intent.getExtras().get("data")您需要键入 cast toBitmap从相机捕获图像。

case 0:
        if (resultCode == Activity.RESULT_OK) {
        Bitmap bitmapImage = (Bitmap) imageReturnedIntent.getExtras().get("data");
        imageView.setImageBitmap(bitmapImage);

        }

        break;
    case 1:
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImage = imageReturnedIntent.getData();
            imageView.setImageURI(selectedImage);
        }
        break;
    }
于 2013-10-25T12:01:00.920 回答
0

还请记住,有一个错误(主要在三星设备上注意到,我没有当前信息是否已修复)getData()返回 null。如果是这种情况,您需要使用预先插入的 Uri,如下所示:

preinsertedUri = getContentResolver().insert(        
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());

稍后在onActivityResult()方法中:

Uri imageUri = preinsertedUri;

这样我就可以防止Uri在某些三星设备上为空

于 2013-10-25T12:33:54.097 回答