在我的应用中,当用户单击图像视图时,用户可以从图库中选择图像或从相机捕获图像并将其显示在图像视图上.如果从图库中选择了图像,则可以在imageview上显示图像,但是如果捕获了图像,则无法显示.如果捕获了图像,则imageUri为null.
谁能帮我解决这个问题?
private final static int PICK_IMAGE_REQUEST = 1;
private final static int CAMERA = 2;
private Uri imageUri;
private void takePhotoFromCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,CAMERA);
}
public void selectImage() {
Intent photoPickerIntent = new Intent();
photoPickerIntent.setType("image/*");
photoPickerIntent.setAction(Intent.ACTION_PICK);
startActivityForResult(photoPickerIntent,PICK_IMAGE_REQUEST);
}
@Override
public void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode,resultCode,data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
imageUri = data.getData();
Picasso.get().load(imageUri).into(circleImageView);
System.out.println("haha pic " + imageUri);
}
if (requestCode == CAMERA && resultCode == RESULT_OK && data != null && data.getData() != null){
imageUri = data.getData();
Picasso.get().load(imageUri).into(circleImageView);
System.out.println("haha camera " + imageUri);
}
}
最佳答案
我建议您将图像显示为imageview上的位图,然后将其转换为url形式并存储到数据库中.
原文链接:https://www.f2er.com/android/531343.htmlpublic Uri getImageUri(Context inContext,Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG,100,bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(),inImage,"Title",null);
return Uri.parse(path);
}
在您的onActivityResult方法中调用此方法,并将此代码放入其中
Bundle extras = data.getExtras();
bitmap = (Bitmap) extras.get("data");
circleImageView.setImageBitmap(bitmap);
imageUri = getImageUri(getApplicationContext(),bitmap);