我想加载从画廊和相机拍摄的图像,当用户点击捕捉按钮时,图像将从照相手机设备捕捉,当用户点击 add_gallery 按钮时,图像将从画廊拍摄,我的问题是当我从图库中选择一张图片,它不会显示在我的 Activity 中(在由 foto_dp 命名的我的 ImageView 上)。 这是我的代码:
@Override
public void onClick(View arg0) {
switch (arg0.getId()){
break;
case R.id.capture_dp:
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
startActivityForResult(intent, TAKE_PHOTO_CODE);
break;
case R.id.add_galery_dp:
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
startActivityForResult(i, PICK_FROM_GALLERY);
break;
case R.id.delete_image_dp:
break;
}
public Uri setImageUri() {
// Store image in dcim
File file = new File(Environment.getExternalStorageDirectory() +"/android/data/spaj_foto/spaj_foto("+counter+").png");
Uri imgUri = Uri.fromFile(file);
this.imgPath = file.getAbsolutePath();
return imgUri;
}
public String getImagePath() {
return imgPath;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_CANCELED) {
if (requestCode == TAKE_PHOTO_CODE) {
selectedImagePath = getImagePath();
foto_dp.setImageBitmap(decodeFile(selectedImagePath));
if (requestCode == PICK_FROM_GALLERY) {
selectedImagePath = getImagePath();
foto_dp.setImageBitmap(decodeFile(selectedImagePath));
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
public Bitmap decodeFile(String path) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeFile(path, o2);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
我的代码有问题吗?以及如何使用我的 Activity 按钮删除图像?我希望有人能帮助我解决我的问题,谢谢
最佳答案
您不需要解码文件。试试下面的代码
private final int REQUEST_CODE_CAMERA_IMAGE = 1000;
private final int REQUEST_CODE_EXTERNAL_IMAGE = 2000;
//select picture from external storage
btnChoosePicture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// choose picture from gallery
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent,
REQUEST_CODE_EXTERNAL_IMAGE);
}
});
//take picture from camera
btnTakePhoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// start camera to take picture
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
setImageUri());
startActivityForResult(intent,
REQUEST_CODE_CAMERA_IMAGE);
}
});
像这样显示图像
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
// get image from camera
case REQUEST_CODE_CAMERA_IMAGE:
if (resultCode == Activity.RESULT_OK) {
imageView.setImageUri(setImageUri());
}
break;
// get image from external storage
case REQUEST_CODE_EXTERNAL_IMAGE:
if (resultCode == Activity.RESULT_OK) {
imageView.setImageUri(data.getData());
}
break;
default:
break;
}
}
删除图片
btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
File file = new File(imagePath);
if(file.exist())
file.delete();
}
});
关于java - 机器人 : how to add image from gallery and delete image with a Button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20875763/