您的位置:首页 > 移动开发 > Android开发

Android调用系统相机拍照、裁剪,并保存到手机SD卡中,展示到界面

2014-12-04 13:50 375 查看
这是一个完整的例子,复制就能运行



//布局文件:

<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:orientation="vertical" >

<ImageView

android:id="@+id/im_picture"

android:layout_width="100dp"

android:layout_height="100dp"

android:src="@drawable/ic_launcher"

android:layout_gravity="center_horizontal"

/>

<Button

android:id="@+id/btn_take_photo"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="拍照" />

</LinearLayout>

//Java代码:

package com.zhh.android;

import java.io.File;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.provider.MediaStore;

import android.provider.MediaStore.Audio.Media;

import android.app.Activity;

import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

public class MainActivity extends Activity {

public static final int TAKE_PHOTO = 1;

public static final int CROP_PHOTO = 2;

private ImageView im_picture;

private Button btn_takePhoto;

private Uri imageUri;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

im_picture = (ImageView) findViewById(R.id.im_picture);

btn_takePhoto = (Button) findViewById(R.id.btn_take_photo);

btn_takePhoto.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// 创建File对象,用于存储拍照后的照片

File outputImage = new File(Environment

.getExternalStorageDirectory(), "tempImage.jpg");

try {

if (outputImage.exists()) {

outputImage.delete();

}

outputImage.createNewFile();

} catch (Exception e) {

e.printStackTrace();

}

imageUri = Uri.fromFile(outputImage);

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

startActivityForResult(intent, TAKE_PHOTO);// 启动相机程序

}

});

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {

case TAKE_PHOTO:

if (resultCode == RESULT_OK) {

Intent intent = new Intent("com.android.camera.action.CROP");

intent.setDataAndType(imageUri, "image/*");

intent.putExtra("scale", true);

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

startActivityForResult(intent, CROP_PHOTO);// 启动裁剪程序

}

break;

case CROP_PHOTO:

try {

if (resultCode == RESULT_OK) {

Bitmap bitmap = BitmapFactory

.decodeStream(getContentResolver().openInputStream(

imageUri));

im_picture.setImageBitmap(bitmap);// 将裁剪后的图片显示出来

}

} catch (Exception e) {

e.printStackTrace();

}

break;

}

}

}// class

//主清单文件中配置写入SD卡权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: