2

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

Aoyama Nanami
  • 2,532
  • 9
  • 32
  • 50

2 Answers2

1

You do not need to decode file. Try following code

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);

        }
    });

Display image like this

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;
    }

}

To delete image

btnDelete.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
          File file = new File(imagePath);
          if(file.exist())
              file.delete();    
    }
 });
Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45
  • i need to decode my image, because when my image saved in sd card, the size is 0KB, btw can you help me to implement it with my code? – Aoyama Nanami Jan 02 '14 at 04:08
0

When you get image url from gallery, just delete it like file

button.setOnClickListener(deleteListener);
OnClickListener deleteListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub  
        File file = new File(imagePath);
        file.delete();
    }
};

Show image use imageview

Community
  • 1
  • 1
henry4343
  • 3,871
  • 5
  • 22
  • 30