在同一个Activity上,我有三张卡片,每张卡片都有一个ImageButton和一个看起来像这样的ImageView:
按下相机按钮时,我希望相机拍摄照片并将拍摄的图像指定给特定的ImageView。 到目前为止我所拥有的是:
我为每个按钮创建了一个OnTouchEventListener,如下所示:
ImageButton camera1 = (ImageButton) findViewById(R.id.camera_button1);
camera1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// action started modify alpha
v.setAlpha((float)0.5);
break;
case MotionEvent.ACTION_UP:
// touch ended modify alpha back
// Toast.makeText(context, "Touch ended", Toast.LENGTH_SHORT).show();
v.setAlpha((float)1.0);
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//intent.putExtra("imageViewName", "front");
//bundle.putString("camera1", "front");
Bundle bundle = new Bundle();
bundle.putString("camera1", "front");
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
break;
default:
break;
}
return false;
}
});
两者都没有,使用intent.putExtra(“test”,“test”);或intent.putExtra(捆绑)工作。我为每个意图添加字符串的原因是能够将该字符串用作标志并在:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Bundle bundle = null;
bundle = getIntent().getExtras();
// Get the bundle and extract strings
String test = bundle.getString("camera1");
// Create a thumbnail
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
// Detect where to assign the view
switch ((String)data.getExtras().get("imageViewName")) {
case "front":
front.setImageBitmap(thumbnail);
break;
case "message":
message.setImageBitmap(thumbnail);
break;
case "address":
address.setImageBitmap(thumbnail);
break;
case "other1":
other1.setImageBitmap(thumbnail);
break;
case "other2":
other2.setImageBitmap(thumbnail);
break;
case "other3":
other3.setImageBitmap(thumbnail);
break;
default:
break;
}
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
try {
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
// Get the return data from the camera
}
尝试捕捉该意图并选择ImageView分配从相机推送的图像。基本上,我希望能够在用户点击ImageView1的按钮时让图像从相机返回到附加到ImageView。我该如何处理?
答案 0 :(得分:3)
选项一:
将不同的常量传递给相机活动,例如
startActivityForResult(it, REQ_SCAN + imgIndex);
然后在你的
中抓住它 @Override
protected void onActivityResult(int request, int result, Intent data) {
switch (request) {
case REQ_SCAN + 0:
case REQ_SCAN + 1:
case REQ_SCAN + 2: break;
imgIndex = request - REQ_SCAN;
}
...
三个不同的值应该根据您的要求唯一标识您的图片。
选项二:
启动相机时,请创建一个具有唯一名称的临时文件,将其交给相机并保留名称。
tmpFl = File.createTempFile(TMP_FILENM, null, context.getExternalCacheDir());
it.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tmpFl));
startActivityForResult(it, REQ_SCAN);
mTmpFlNm = tmpFl.getAbsolutePath();
}
当它回到'onActivityResult()'
时 case REQ_SCAN: {
if (result == Activity.RESULT_OK) {
try {
tmpFl = new File(mTmpFlNm);
byte[] jpgBuff = UT.file2Bytes(tmpFl));
} finally { if (tmpFl != null) tmpFl.delete(); }
}
break;
只需获取你提交给cam活动的临时文件,获取它的内容(它是一个带有jpg数据的缓冲区)并将其解压缩到位图(或用它做其他事情 - 上传,重命名,保存......)
因此,在您的情况下,您可能会使用3个不同的文件名。 OPTION ONE可能更适合您的需求。
祝你好运