views:

28

answers:

2

Hi I am trying to create a Custom View that takes as well as deletes a picture and looks like the following:

alt text

Following operations a view can perform:

  • Take a picture (by clicking on the No Image Available sign) -- works
  • Delete a picture (by clicking on the red cross) -- works
  • Preview a picture

How do I create a listener to find out when the image has been taken by the camera? The location of the image that needs to be taken is known before.

public class artooPicture extends RelativeLayout {

    ImageView preview, delete;
    String value = Environment.getExternalStorageDirectory() + "/99006796"
            + "_" + ".jpg", suffix;
    boolean clicked = false;

    public artooPicture(Context context) {
        this(context, null);

    }

    public artooPicture(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public artooPicture(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        ((Activity) getContext()).getLayoutInflater().inflate(R.layout.picture,
                this, true);

        delete = (ImageView) findViewById(R.id.scan_delete);
        preview = (ImageView) findViewById(R.id.scan_preview);

        File f = new File(value);
        if (f.exists()) {
            clicked = true;
            preview.setImageDrawable(BitmapDrawable.createFromPath(value));

        } else {

        }

        delete.setOnClickListener(deleteListener);
        preview.setOnClickListener(previewListener);
    }

    View.OnClickListener deleteListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            if(clicked){
                File f = new File(value);
                f.delete();
                preview.setImageResource(R.drawable.no_image);
            }

            clicked = false;
        }
    };

    View.OnClickListener previewListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(value)));
            getContext().startActivity(i);

        }
    };

}
A: 

Solved using Threads. Does anyone have a better solution?

View.OnClickListener previewListener = new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (!clicked) {

                Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                i.putExtra(MediaStore.EXTRA_OUTPUT, Uri
                        .fromFile(new File(value)));
                getContext().startActivity(i);

                Thread t = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        File f;
                        do {
                            f = new File(value);
                        } while (!f.exists());
                        if (f.exists()) {
                            preview.setImageDrawable(BitmapDrawable
                                    .createFromPath(value));
                            clicked = true;
                        }
                    }
                });
                t.run();

            } else {
                Dialog d = new Dialog(getContext());
                ImageView iv = new ImageView(getContext());
                iv.setImageDrawable(BitmapDrawable.createFromPath(value));              
                d.setContentView(iv);
                d.setCancelable(true);
                d.show();
            }
        }
    };
Sameer Segal
A: 

Why don't you use startActivityForResult() and set the preview in your

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
     Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
     ImageView image = new ImageView(this.getApplicationContext());
     image.setImageBitmap(thumbnail);
}

method?

Cpt.Ohlund
I am creating a view that can be embedded anywhere. From a View you can start an activity but not an activityForResult
Sameer Segal