gpt4 book ai didi

java - Android 中如何一键打开相机?

转载 作者:行者123 更新时间:2023-12-01 12:56:59 25 4
gpt4 key购买 nike

我在 Android 中创建了相机应用程序。我创建了一项 Activity ,同一 Activity 中包含 3 个按钮和 3 个 ImageView 。当我必须单击第一个按钮时,该快照将显示在第一个 ImageView 中,然后当我单击第二个按钮时,该快照将显示在第二个 ImageView 中,与第三个按钮相同。

当我运行我的应用程序时,相机完全打开并拍摄快照,但它也无法像其他按钮一样显示在 ImageView 中。这是我的代码。

这是我的 Activity 代码

public class Take_Snap_Page extends Activity
{
ImageView imgPersonalSnap;
ImageView imgAddressProofSnap;
ImageView imgPanCardProofSnap;
ImageView imgHideBitmap;

Button btnPersonal ;
Button btnAddress;
Button btnPanCard;
Button btnSubmitSnap;

Bitmap bp;
Bitmap bitmap ;

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.take_snap);

imgPersonalSnap = (ImageView)findViewById(R.id.imagesPersonnalSnap);
imgAddressProofSnap = (ImageView)findViewById(R.id.imageAddressProofSnap);
imgPanCardProofSnap = (ImageView)findViewById(R.id.imagePanCardproofSnap);
imgHideBitmap = (ImageView)findViewById(R.id.imgHide);



btnPersonal = (Button)findViewById(R.id.buttonCapture_Personal_Snap);
btnPersonal.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
open();

imgPersonalSnap.setImageBitmap(bitmap);

}
});




btnAddress = (Button)findViewById(R.id.buttonCapture_AddressSnap);
btnAddress.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
open();

}
});



btnPanCard = (Button)findViewById(R.id.buttonCapture_PanCardSnap);
btnPanCard.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
open();

}
});

}


public void open() {
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 0)
{
if (resultCode == RESULT_OK && data !=null )
{
// ... now let's see use the picture at data/
bp = (Bitmap) data.getExtras().get("data");
imgHideBitmap.setImageBitmap(bp);
BitmapDrawable drawable = (BitmapDrawable) imgHideBitmap.getDrawable();
bitmap = drawable.getBitmap();


}
}
}
}

最佳答案

尝试使用requestCode,如下所示。

btnAddress.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
open(0);

}
});

btnAddress.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
open(1);

}
});

btnPanCard.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
open(2);

}
});

public void open(int requestCode) {
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, requestCode);
}

还有

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 0)
{
if (resultCode == RESULT_OK && data !=null )
{
// ... now let's see use the picture at data/
switch(requestCode){
case 0:
bp = (Bitmap) data.getExtras().get("data");
imgPersonalSnap.setImageBitmap(bp);
BitmapDrawable drawable = (BitmapDrawable) imgHideBitmap.getDrawable();
bitmap = drawable.getBitmap();
break;
case 1:
bp = (Bitmap) data.getExtras().get("data");
imgAddressProofSnap.setImageBitmap(bp);
BitmapDrawable drawable = (BitmapDrawable) imgHideBitmap.getDrawable();
bitmap = drawable.getBitmap();
break;
case 2:
bp = (Bitmap) data.getExtras().get("data");
imgPanCardProofSnap.setImageBitmap(bp);
BitmapDrawable drawable = (BitmapDrawable) imgHideBitmap.getDrawable();
bitmap = drawable.getBitmap();
break;

}

}
}
}

希望这对您有帮助。让我知道发生了什么

关于java - Android 中如何一键打开相机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23798167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com