简体   繁体   English

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

[英]How to open the camera with more than one button click in Android?

I created camera application in Android. 我在Android中创建了相机应用程序。 I have created one activity and 3 buttons and 3 image view are included in the same activity. 我创建了一个活动,并且同一活动中包含3个按钮和3个图像视图。 When I have to take snap click on first button that snap will display in first image view, then when I take snap on click second button that snap will display in second image view and same as to third button. 当我必须按一下快照时,单击快照将在第一个图像视图中显示,然后当我按一下单击第二个按钮时,快照将在第二个图像视图中显示,与第三个按钮相同。

When I run my app camera open success fully and take the snap also done but it can't display in image view same as to another buttons also. 当我运行我的应用相机时,完全打开成功并完成了快照,但是它在图像视图中的显示也无法与其他按钮相同。 Here is my code. 这是我的代码。

Here is my Activity Code 这是我的活动代码

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


                }
        }
    }
}

Try wit requestCode as like following. 尝试使用wit 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);

        }
    });

And

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

And

@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;

                }

            }
    }
}

I hope this will help you. 我希望这能帮到您。 Let me know what happend 让我知道发生了什么

try this, 尝试这个,

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;

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM