我正在尝试拍照并在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()。
提前致谢。
答案 0 :(得分:0)
试试这个
始终使用intent.getExtras().get("data")
您需要输入强制转换为Bitmap
以从相机中捕获图像。
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;
}
答案 1 :(得分:0)
还要记住,在getData()
返回null的情况下,存在一个错误(主要是在三星设备上注意到,我没有关于它是否已修复的当前信息)。在这种情况下,您需要使用预先插入的Uri:
preinsertedUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
稍后在onActivityResult()
方法:
Uri imageUri = preinsertedUri;
这样我就能阻止某些三星设备上的Uri
为空
答案 2 :(得分:-1)
刚刚在清单中添加了这些行:
android:configChanges="keyboardHidden|orientation|screenSize"