以下是activity中的代码,代码如下:
package com.fk.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class TakePhotoActivity extends Activity {
private Button takePhoto;
private ImageView pic;
//自定义变量
public static final int TAKE_PHOTO = 1;
public static final int CROP_PHOTO = 2;
private Uri imageUri; //图片路径
private String filename; //图片名称
private File outputImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_takephoto);
initView();
initData();
}
private void initView() {
takePhoto = (Button) findViewById(R.id.button1);
pic = (ImageView) findViewById(R.id.imageview1);
}
private void initData() {
takePhoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 图片名称用时间命名
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
// 获取当前系统时间,用毫秒来计算
Date date = new Date(System.currentTimeMillis());
// 转化格式
filename = sdf.format(date);
// 存储至DCIM文件夹,先获取路径(这里注意不是自己创建的文件夹)
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
// 获取每张图片的路径
outputImage = new File(path, filename + ".jpg");
// 判断当前文件是否存在,如果已经存在,就删除,如果不存在,就创建此文件
try {
if(outputImage.exists()){
outputImage.delete();
}
// 如果不存在,就创建新的文件
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
// 将file转化成Uri,并启动照相程序
imageUri = Uri.fromFile(outputImage);
// 照相
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// 指定图片的输出地址
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
// 启动照相
startActivityForResult(intent, TAKE_PHOTO);
}
});
}
// 通过onActivityResult完成拍照截图和保存
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode!=RESULT_OK){
Toast.makeText(this, "相机错误", Toast.LENGTH_SHORT).show();
return;
}
switch (requestCode) {
case TAKE_PHOTO:
System.out.println(imageUri);
// pic.setImageURI(imageUri);
// Bitmap b = BitmapFactory.decodeFile(imageUri.getPath());
// Bitmap b = BitmapFactory.decodeFile(outputImage.getAbsolutePath());
// pic.setImageBitmap(b);
// 裁剪
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imageUri, "image/*");
intent.putExtra("scale", true);
// 设置宽高比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// 设置图片裁剪宽高
intent.putExtra("outputX", 340);
intent.putExtra("outputY", 340);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
// 广播刷新相册
Intent intentBC = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intentBC.setData(imageUri);
this.sendBroadcast(intentBC);
// 特别注意:这里的第一个参数一定要用intent,不能用intentBC,因为我们是启动进入裁剪页面。
// 而intentBC是为了发送广播。这里一定要注意
startActivityForResult(intent, CROP_PHOTO);
break;
case CROP_PHOTO:
// 把图片解析成bitmap对象
try {
System.out.println("走你,走我了吗?");
Bitmap bm = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
// 将裁剪后的图片显示出来
pic.setImageBitmap(bm);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
break;
default:
break;
}
}
}
以下是xml的代码:
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TakePhoto"
/>
<ImageView
android:id="@+id/imageview1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
<span style="white-space:pre"> </span>而后我们在清单文件中配置一下,添加一下读写SD卡的权限,还有拍照的权限,最后,注册下activity,就可以实现完美的拍照功能了。