您好我正在尝试创建一个自定义视图,它可以删除图片,如下所示:
以下操作可以执行以下操作:
如何创建一个监听器来查找相机拍摄图像的时间?之前已知需要拍摄的图像的位置。
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);
}
};
}
答案 0 :(得分:0)
使用线程解决。有没有人有更好的解决方案?
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();
}
}
};
答案 1 :(得分:0)
为什么不使用startActivityForResult()
并在
@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);
}
方法