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"/>
//布局文件:
<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"/>
相关文章推荐
- Android调用系统相机拍照、从相册中取照片,裁剪,并保存到手机SD卡中,展示到界面
- android 调用系统相机拍照,并保存到SD卡
- Android调用自身相机拍照并保存拍摄的照片到手机的sd卡
- android 调用系统相机拍照,并保存到SD卡
- android调用系统相机拍照保存照片并显示在当前界面
- android调用系统相机和相册进行拍照裁剪处理,解决不同安卓版本存在无法加载相册的问题,处理了是否有sd卡的存在的情况
- Android 调用系统相机、图库 ,取图片并裁剪、保存sd卡
- Android手机调用系统相机拍照、裁剪以及获取Url上传图片
- android调用系统的相机拍照 、裁剪,然后保存
- Android调用系统相机拍照并保存到SD卡的两种实现方式
- Android基础--调用系统相机,打开相册,裁剪图片并展示到界面
- Android 调用系统相机拍照保存以及调用系统相册的方法
- android调用系统相机拍照并保存在本地
- Android 调用系统相机拍照保存以及调用系统相册的方法
- Android 调用系统相机拍照保存以及调用系统相册的方法
- Android调用相机实现拍照并裁剪图片,调用手机中的相册图片并裁剪图片
- Android 拍照后调用系统裁剪界面
- 调用android系统相机拍照并保存
- Android 调用系统相机拍照保存以及调用系统相册的方法
- 调用android系统相机拍照并保存