我正在尝试拍照并在 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() 一直被调用。
提前致谢。