如何调用自己手机的拍照功能以及从手机相册选择图片? 比如找个小例子:
首先,还是弄一个layout先:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/take_photo"
android:layout_alignRight="@+id/choose_from_album"
android:layout_below="@+id/choose_from_album"
android:layout_marginTop="55dp"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/choose_from_album"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/take_photo"
android:layout_alignBottom="@+id/take_photo"
android:layout_alignParentRight="true"
android:layout_marginRight="28dp"
android:text="从相册选择图片" />
<Button
android:id="@+id/take_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="23dp"
android:text="拍照" />
</RelativeLayout>
对于该活动,其代码:
public class MainActivity extends Activity {
public static final int TAKE_PHOTO = 1;
public static final int CROP_PHOTO = 2; //裁剪图片
//用于拍照
private Button takePhoto;
private ImageView picture;
private Uri imageUri;
//选择图片
private Button chooseFromAlbum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takePhoto = (Button)findViewById(R.id.take_photo);
picture = (ImageView)findViewById(R.id.picture);
takePhoto.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
//创建File对象,用于存储拍照后的图片
File outputImage = new File(Environment.getExternalStorageDirectory(),"tempImage.jpg");//参数: 目录 文件
try {
if(outputImage.exists()){ //文件已经存在就删除
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException 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);//启动拍照服务,而且需要返回结果,返回数据由onActivityResult处理
}
});
chooseFromAlbum = (Button)findViewById(R.id.choose_from_album);
chooseFromAlbum.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//创建File文件,用于存储选择的照片
File outputImage = new File(Environment.getExternalStorageDirectory(),"output_image.jpg");
try {
if(outputImage.exists()){
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImage); //打开相册
Intent intent = new Intent("android.intent.action.GET_CONTENT");//启动从相册选择图片功能
intent.setType("image/*");
intent.putExtra("crop", true);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CROP_PHOTO);
}
});
}
//用于响应调用活动返回
protected void onActivityResult(int requestCode, int resultCode, Intent 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:
if(resultCode == RESULT_OK){
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); //跨程序共享数据,共享输入数据
picture.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}
}
在配置文件中要配置权限说明:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>