Android打开相册和摄像头

在xml布局文件中布局

 <ImageView 
	    android:id="@+id/ivHead"
	    android:layout_marginTop="20dp"
	    android:layout_centerHorizontal="true"
	    android:layout_width="150dp"
	    android:layout_height="150dp"/>
	
	<Button 
	    android:id="@+id/btnSelectImage"
	    android:layout_marginTop="200dp"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:text="打开相册"/>
	
	<Button 
	    android:id="@+id/btnTakePhoto"
	    android:layout_marginTop="260dp"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:text="拍照"/>

在对应的Java文件中

package com.example.meida;

import android.os.Bundle;

import java.io.File;

import com.example.meida.MainActivity;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class openphoto extends Activity implements OnClickListener {
	
	private ImageView ivHead;
	private Button btnSelectImage, btnTakePhoto;
	
	private static final int IMAGE_CODE=0;//打开相册
	private static final int CAMERA_CODE=1;//相机
	private static final int RESIZE_CODE=2;//调整大小
	
	private static final String IMAGE_NAME=" ";//图片字符串

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

		// 初始化控件
		init();

		// 绑定点击事件
		bindClick();
	}

	// 初始化控件
	private void init() {
		ivHead = (ImageView) findViewById(R.id.ivHead);
		btnSelectImage = (Button) findViewById(R.id.btnSelectImage);
		btnTakePhoto = (Button) findViewById(R.id.btnTakePhoto);
	}

	// 绑定点击事件
	private void bindClick() {
		btnSelectImage.setOnClickListener(this);
		btnTakePhoto.setOnClickListener(this);

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btnSelectImage:
			Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
			galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
			galleryIntent.setType("image/*");//图片
			startActivityForResult(galleryIntent, IMAGE_CODE);   //跳转,传递打开相册请求码
			break;

		case R.id.btnTakePhoto:
			if (isSdcardExisting()) {
				Intent cameraIntent = new Intent(
						"android.media.action.IMAGE_CAPTURE");//拍照
				cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
				cameraIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
				startActivityForResult(cameraIntent, CAMERA_CODE);   //跳转,传递拍照请求码
			} else {
				Toast.makeText(view.getContext(), "请插入sd卡", Toast.LENGTH_LONG)
						.show();
			}
			break;
		default:
			break;
		}

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != RESULT_OK) {
			return;
		} else {
			switch (requestCode) {
			case IMAGE_CODE:
				resizeImage(data.getData());
				break;
			case CAMERA_CODE:
				if (isSdcardExisting()) {
					resizeImage(getImageUri());
				} else {
					Toast.makeText(openphoto.this, "未找到存储卡,无法存储照片!",
							Toast.LENGTH_LONG).show();
				}
				break;

			case RESIZE_CODE:
				if (data != null) {
					showResizeImage(data);
				}
				break;
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	//判断SD卡是否存在
	private boolean isSdcardExisting(){
		final String state=Environment.getExternalStorageState();
		if(state.equals(Environment.MEDIA_MOUNTED)){
			return true;
		}else{
			return false;
		}
	}
	
	//重塑图片大小
	public void resizeImage(Uri uri){
		Intent intent=new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		intent.putExtra("crop","true");//可以裁剪
		intent.putExtra("aspectX",1);
		intent.putExtra("aspectY",1);
		intent.putExtra("outputX",150);
		intent.putExtra("outputY",150);
		intent.putExtra("return-data",true);
		startActivityForResult(intent, RESIZE_CODE);//跳转,传递调整大小请求码
	}
	
	//获取路径
	private Uri getImageUri(){
		return Uri.fromFile(new File(Environment.getExternalStorageDirectory(),IMAGE_NAME));
	}

	//显示图片
	private void showResizeImage(Intent data){
		Bundle extras=data.getExtras();
		if(extras!=null){
			Bitmap photo=extras.getParcelable("data");
			Drawable drawable=new BitmapDrawable(photo);
			ivHead.setImageDrawable(drawable);
		}
	}
}

一定要注意xml和Java文件是否对应

你可能感兴趣的:(Android打开相册和摄像头)