Android拍摄照片并在Activity中创建带有照片的小画廊

时间:2015-10-18 01:15:05

标签: android android-intent imageview android-camera

我正在建立一个用户按下按钮的活动,然后将它们带到一个屏幕,在那里他们可以使用默认的相机应用程序拍摄照片,然后将其添加到位图列表中以便稍后存储对象。它还会显示在“活动”屏幕上拍摄的这些照片。然而,我目前的方式设置方式只允许单个imageview照片出现,我想让它,以便用户可以继续拍照,它将继续在活动上显示它们,但也将保持将它们添加到列表中。 这是代码:

public class MainActivity extends Activity implements OnClickListener {

    ImageView ivThumbnailPhoto;
    Bitmap bitMap;
    static int TAKE_PICTURE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnTackPic = (Button) findViewById(R.id.btnTakePic);
        ivThumbnailPhoto = (ImageView) findViewById(R.id.ivThumbnailPhoto);

        if(hasCamera()){
            tvHasCamera.setBackgroundColor(0xFF00CC00);
            tvHasCamera.setText("You have Camera");
        }

        if(hasDefualtCameraApp(MediaStore.ACTION_IMAGE_CAPTURE)){
            tvHasCameraApp.setBackgroundColor(0xFF00CC00);
            tvHasCameraApp.setText("You have Camera Apps");
        }

        btnTackPic.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        startActivityForResult(intent, TAKE_PICTURE);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

        if (requestCode == TAKE_PICTURE && resultCode== RESULT_OK && intent != null){
            // get bundle
            Bundle extras = intent.getExtras();

            // get bitmap
            bitMap = (Bitmap) extras.get("data");
            ivThumbnailPhoto.setImageBitmap(bitMap);

        }
    }

    private boolean hasCamera(){
        return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
    }

    private boolean hasDefualtCameraApp(String action){
        final PackageManager packageManager = getPackageManager();
        final Intent intent = new Intent(action);
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);

        return list.size() > 0;

    }
}

Layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    tools:context=".MainActivity"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/thumbnailPhoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />

    <Button
        android:id="@+id/takePicButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Take a Picture" />

</LinearLayout>

我假设我可以使用Gallery而不是ImageView来执行此操作,但我很难实现这样的解决方案,所以任何帮助都会很棒 感谢

1 个答案:

答案 0 :(得分:0)

您可以在将其解码为位图后使用uri显示活动

public class MainActivity extends Activity implements OnClickListener {

        ImageView ivThumbnailPhoto;
        Bitmap bitMap;
        static int TAKE_PICTURE = 1;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            btnTackPic = (Button) findViewById(R.id.btnTakePic);
            ivThumbnailPhoto = (ImageView) findViewById(R.id.ivThumbnailPhoto);

            if(hasCamera()){
                tvHasCamera.setBackgroundColor(0xFF00CC00);
                tvHasCamera.setText("You have Camera");
            }

            if(hasDefualtCameraApp(MediaStore.ACTION_IMAGE_CAPTURE)){
                tvHasCameraApp.setBackgroundColor(0xFF00CC00);
                tvHasCameraApp.setText("You have Camera Apps");
            }

            btnTackPic.setOnClickListener(this);

        }

        @Override
        public void onClick(View view) {

           Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
           startActivityForResult(Intent.createChooser(intent, "Select Picture"), TAKE_PICTURE);

        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

            if (requestCode == TAKE_PICTURE && resultCode== RESULT_OK && intent != null){

                // get Uri
                ivThumbnailPhoto.setImageUri(intent.getData());

            }
        }

        private boolean hasCamera(){
            return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
        }

        private boolean hasDefualtCameraApp(String action){
            final PackageManager packageManager = getPackageManager();
            final Intent intent = new Intent(action);
            List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);

            return list.size() > 0;

        }
    }