如何在Android中点击多个按钮打开相机?

时间:2014-05-22 05:02:17

标签: java android

我在Android中创建了相机应用程序。我创建了一个活动,3个按钮和3个图像视图包含在同一个活动中。当我必须快速单击第一个按钮时,快照将显示在第一个图像视图中,然后当我按下快照时,单击第二个按钮,快照将显示在第二个图像视图中,与第三个按钮相同。

当我运行我的应用程序相机时,完全打开成功并完成快照,但它也无法在图像视图中显示,也与其他按钮相同。这是我的代码。

这是我的活动代码

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


                }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

尝试使用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;

                }

            }
    }
}

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

答案 1 :(得分:0)

试试这个,

package com.example.linkedin;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.MediaColumns;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

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 ;

    private int FIRSTCLICK=1;
    private int SECONDCLICK=2;
    private int THIEDCLICK=3;

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




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

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

            }
        });



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

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

            }
        });

    }


    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 == FIRSTCLICK)
        {
            if (resultCode == RESULT_OK && data !=null )
            {
                // ... now let's see use the picture at data/
                imgPersonalSnap.setImageBitmap(decodeFile(getAbsolutePath(data.getData())));
            }
        }else if (requestCode == FIRSTCLICK)
        {
            if (resultCode == RESULT_OK && data !=null )
            {
                // ... now let's see use the picture at data/
                imgAddressProofSnap.setImageBitmap(decodeFile(getAbsolutePath(data.getData())));
            }
        }else if (requestCode == FIRSTCLICK)
        {
            if (resultCode == RESULT_OK && data !=null )
            {
                // ... now let's see use the picture at data/
                imgPanCardProofSnap.setImageBitmap(decodeFile(getAbsolutePath(data.getData())));
            }
        }
    }

    public String getAbsolutePath(Uri uri) {
        String[] projection = { MediaColumns.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }

    public Bitmap decodeFile(String path) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, o);
            // The new size we want to scale to
            final int REQUIRED_SIZE = 70;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeFile(path, o2);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;

    }

}