i want to load image that taken from galllery and camera, when user click on capture button, image will be capture from camera phone device, when user click on add_gallery button, image will be taken from gallery, my problem is when i choose an image from gallery, it is not display on my activity (on My ImageView named by foto_dp). this is my code :
@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;
}
is there anywrong with my code? and how to delete image with a button on my activity? i hope someone can help me to solve my problem, thank you