簡體   English   中英

如何在Android中單擊多個按鈕以打開相機?

[英]How to open the camera with more than one button click in 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();


                }
        }
    }
}

嘗試使用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);

        }
    });

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;

                }

            }
    }
}

我希望這能幫到您。 讓我知道發生了什么

嘗試這個,

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