下面是一些有点工作的代码......我目前在屏幕上有2个ImageViews,下面各有一个按钮。单击该按钮时,相机功能将打开。我的目标是一个图像视图下方的按钮改变了该图像视图,让我能够然后单击另一个按钮来改变另一个。然而,在我改变一个时,它们都变成了同一张照片。我之前曾尝试过几个'if'语句,将每个语句初始化为一个名为“currentImage”的变量,但无济于事。
有什么想法吗?
public class NowThen extends Activity {
Bitmap photo;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nowthen);
}
public void nowPhoto (View view){
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent,0);
}
public void thenPhoto (View view){
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent,0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK) {
if (data != null) {
photo = (Bitmap) data.getExtras().get("data");
ImageView nowImage = (ImageView) findViewById(R.id.nowImage);
ImageView thenImage = (ImageView) findViewById(R.id.thenImage);
nowImage.setImageBitmap(photo);
thenImage.setImageBitmap(photo);
Log.d("camera ---- > ", "" + data.getExtras().get("data"));
}
}
}
}
答案 0 :(得分:0)
您的代码是正确的,除非您使用相同的requestCode 0启动intent。因此,在onActivityResult()块中调用相同的块。
startActivityForResult(intent,0);//for first button
startActivityForResult(intent,1);//for second button
现在在if或switch语句中检查onAcitivityResult()代码中的requestCode。
答案 1 :(得分:0)
将您的thenPhoto代码更改为:
public void thenPhoto (View view){
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent,1);
}
将onActivityResult更改为:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK) {
if (data != null) {
photo = (Bitmap) data.getExtras().get("data");
ImageView nowImage = (ImageView) findViewById(R.id.nowImage);
nowImage.setImageBitmap(photo);
Log.d("camera ---- > ", "" + data.getExtras().get("data"));
}
} else if (requestCode == 1 && resultCode == RESULT_OK) {
if (data != null) {
photo = (Bitmap) data.getExtras().get("data");
ImageView thenImage = (ImageView) findViewById(R.id.thenImage);
thenImage.setImageBitmap(photo);
Log.d("camera ---- > ", "" + data.getExtras().get("data"));
}
}
}
}