1、调用系统摄像头
安卓中,调用系统摄像头其实很简单,只要通过创建一个action为android.media.action.IMAGE_CAPTURE的意图
,然后再调用Intent的putExtra()方法指定图片的输出地址,最后再调用startActivityForResult()来启动活动。由于
我们使用的是一个隐式意图,系统会找到能够响应这个Intent的活动去启动,这样照相机程序就会打开,拍下的照片就会
输出到我们指定的位置。
File outputImageFile = new File(Environment.getExternalStorageDirectory(),
"output_image.jpg");
try {
if (outputImageFile.exists()) {
// 如果这个文件存在的话
outputImageFile.delete();
}
outputImageFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImageFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PHOTO);
注意:MediaStore.ACTION_IMAGE_CAPTURE="android.media.action.IMAGE_CAPTURE"
由于我们是通过startActivityForResult()来启动活动的,因此拍完照后会有结果返回到onActivityResult()方法中,
这时我们可以通过以下代码获得,拍完照的图片:
data.getExtras();//data指的是返回的带有结果数据的IntentBundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
2、调用系统相册
调用系统相册和调用系统摄像头也是通过一个隐式意图,这个隐式意图的action是android.intent.action.GET_CONTENT,
还要调用Intent的setType(),设置意图的类型为image/*,同样是通过startActivityForResult来开启活动
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent, CHOOSE_PHOTO);
如何获得从相册中选取的图片:
Uri chooseImageUri=data.getData();//这里的data是返回带有结果数据的Intent
try {
Bitmap map = BitmapFactory
.decodeStream(getContentResolver().openInputStream(
chooseImageUri));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
3、裁剪图片
裁剪图片也是通过一个action为com.android.camera.action.CROP的隐式意图,开启系统裁剪图片的程序
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imageUri, "image/*");
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//设置裁剪后的图片,输出的地址,imageUri输出地址对应的Uri
startActivityForResult(intent, CROP_PHOTO);
测试项目所有代码:
布局文件:
<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" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="takephoto"
android:text="拍照" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="choosephoto"
android:text="从相册中选择一张照片" />
<ImageView
android:id="@+id/iv_show_photo"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity:
package com.jsako.systemcamera;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
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.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int TAKE_PHOTO = 0;
private static final int CROP_PHOTO = 1;
private static final int CHOOSE_PHOTO = 2;
private ImageView iv_show_photo;
private Uri imageUri;
private File outputImageFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_show_photo = (ImageView) findViewById(R.id.iv_show_photo);
outputImageFile = new File(Environment.getExternalStorageDirectory(),
"output_image.jpg");
}
public void takephoto(View view) {
try {
if (outputImageFile.exists()) {
// 如果这个文件存在的话
outputImageFile.delete();
}
outputImageFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImageFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PHOTO);
}
public void choosephoto(View view) {
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent, CHOOSE_PHOTO);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case TAKE_PHOTO:
if (resultCode == RESULT_OK) {
/*
* 这段代码用于获取系统照相机 Bundle bundle = data.getExtras(); Bitmap bitmap
* = (Bitmap) bundle.get("data");
*/
// 启动裁剪图片界面
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:
if (resultCode == RESULT_OK) {
try {
Bitmap map = BitmapFactory
.decodeStream(getContentResolver().openInputStream(
imageUri));
iv_show_photo.setImageBitmap(map);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
case CHOOSE_PHOTO:
Uri chooseImageUri=data.getData();
try {
Bitmap map = BitmapFactory
.decodeStream(getContentResolver().openInputStream(
chooseImageUri));
iv_show_photo.setImageBitmap(map);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
break;
}
}
}
来源:https://www.cnblogs.com/Jsako/p/5919437.html