本文实例讲述了Android调用手机拍照功能的方法。分享给大家供大家参考。具体如下:
一、main.xml布局文件:
<&#63;xml version="1.0" encoding="utf-8"&#63;>
二、核心代码:
package com.ljq.test; import java.io.ByteArrayOutputStream; import java.io.File; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class TestActivity extends Activity { private static final int NOnE= 0; private static final int PHOTO_GRAPH = 1;// 拍照 private static final int PHOTO_ZOOM = 2; // 缩放 private static final int PHOTO_RESOULT = 3;// 结果 private static final String IMAGE_UNSPECIFIED = "image/*"; private ImageView imageView = null; private Button btnPhOne= null; private Button btnTakePicture = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageView = (ImageView) findViewById(R.id.imageView); btnPhOne= (Button) findViewById(R.id.btnPhone); btnPhone.setOnClickListener(onClickListener); btnTakePicture = (Button) findViewById(R.id.btnTakePicture); btnTakePicture.setOnClickListener(onClickListener); } private final View.OnClickListener OnClickListener= new View.OnClickListener() { @Override public void onClick(View v) { if(v==btnPhone){ //从相册获取图片 Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED); startActivityForResult(intent, PHOTO_ZOOM); }else if(v==btnTakePicture){ //从拍照获取图片 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment .getExternalStorageDirectory(),"temp.jpg"))); startActivityForResult(intent, PHOTO_GRAPH); } } }; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == NONE) return; // 拍照 if (requestCode == PHOTO_GRAPH) { // 设置文件保存路径 File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg"); startPhotoZoom(Uri.fromFile(picture)); } if (data == null) return; // 读取相册缩放图片 if (requestCode == PHOTO_ZOOM) { startPhotoZoom(data.getData()); } // 处理结果 if (requestCode == PHOTO_RESOULT) { Bundle extras = data.getExtras(); if (extras != null) { Bitmap photo = extras.getParcelable("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0-100)压缩文件 //此处可以把Bitmap保存到sd卡中 imageView.setImageBitmap(photo); //把图片显示在ImageView控件上 } } super.onActivityResult(requestCode, resultCode, data); } /** * 收缩图片 * * @param uri */ public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, IMAGE_UNSPECIFIED); intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY 是裁剪图片宽高 intent.putExtra("outputX", 300); intent.putExtra("outputY", 500); intent.putExtra("return-data", true); startActivityForResult(intent, PHOTO_RESOULT); } }
希望本文所述对大家的Android程序设计有所帮助。