如何在Android上制作照片库?

时间:2016-05-20 19:21:13

标签: android

我想在我的Android应用程序上创建一个带有相机拍摄照片的图库。我已经在Manifest的意图和许可下获得了相机活动,但我只能拍一张照片。这是我的代码:

MyCameraAction.java

    package com.example.android.navigationdrawer;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

public class MyCameraActivity extends AppCompatActivity {


    private static final int CAMERA_REQUEST = 1888;
    private ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_my_camera);
        this.imageView = (ImageView)this.findViewById(R.id.imageView1);
        ImageView photoButton = (ImageView) this.findViewById(R.id.button1);
        photoButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            imageView.setImageBitmap(photo);
        }
    }

}

content_my_camera.xml(非常简单,只有一个按钮和一个imageview)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_menu_camera"
        android:layout_gravity="center_horizontal"
        android:clickable="true"
        android:contentDescription="@string/photoButtonUse"
        />

    <ImageView android:id="@+id/imageView1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:contentDescription="@string/ImageViewGallery"
        />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,我发布了代码:

MyCameraActivity

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MyCameraActivity extends AppCompatActivity {


    private static final int CAMERA_REQUEST = 1888;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_my_camera);
        ImageView photoButton = (ImageView) this.findViewById(R.id.button1);


        //OnClick camera button
        photoButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {

            //Cast LinearLayout of .xml
            LinearLayout CamaraLayout = (LinearLayout) findViewById(R.id.CameraLayout);

            //Create imageView to put Bitmap
            ImageView imageView = new ImageView(this);

            //Adding the Bitmap
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            imageView.setImageBitmap(photo);

            //Set the imageView to layout
            CamaraLayout.addView(imageView);

            //Show layout

        }
    }

}

当你拍摄图像时,它会创建一个新的ImageView并将其位图设置为拍摄的图像