开发者

Custom View that takes & delete a Camera Picture

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

Custom View that takes & delete a Camera Picture

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

        }
    };

}


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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜