Android调用摄像头拍照和从相册中选择(上传、更换头像)
1.布局文件里写两个按钮一个为拍照一个为从相册中选择,还有一个ImageVIew用来放头像
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.choosepictest.MainActivity" >
2.MainActivity中直接贴代码
package com.example.choosepictest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract.Contacts.Data;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
public static final int TAKE_PHOTO=1;
public static final int CROP_PHOTO=2;
private Button takephoto;
private ImageView picture;
private Uri imageUri;
public static final int CHOOSE_PHOTO=3;
private Button choose;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takephoto=(Button) findViewById(R.id.but1);
picture=(ImageView) findViewById(R.id.img);
choose=(Button) findViewById(R.id.but2);
takephoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
File outputImage=new File(Environment.getExternalStorageDirectory(),
"output_image.jpg");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
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);
}
});
choose.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent, CHOOSE_PHOTO);
}
});
}
@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
// TODO Auto-generated method stub
super.onActivityResult(arg0, arg1, arg2);
switch (arg0) {
case TAKE_PHOTO:
if (arg1==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 (arg1==RESULT_OK) {
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
picture.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case CHOOSE_PHOTO:
if (arg1==RESULT_OK) {
if (Build.VERSION.SDK_INT>=19) {
handleImageOnKitKat(arg2);
}else{
handleImageBeforeKitKat(arg2);
}
}
break;
default:
break;
}
}
@SuppressLint("NewApi")
private void handleImageOnKitKat(Intent data) {
// TODO Auto-generated method stub
String imagePath=null;
Uri uri=data.getData();
if (DocumentsContract.isDocumentUri(this, uri)) {
String docId=DocumentsContract.getDocumentId(uri);
if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
String id=docId.split(":")[1];//解析数字格式的ID
String selection=MediaStore.Images.Media._ID+"="+id;
imagePath=getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);
}else if("com.android.providers.downloads.documents".equals(uri.getAuthority())){
Uri contentUri=ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
imagePath=getImagePath(contentUri, null);
}
}else if("content".equalsIgnoreCase(uri.getScheme())){
imagePath=getImagePath(uri, null);
}
displayImage(imagePath);
}
private void handleImageBeforeKitKat(Intent data){
Uri uri=data.getData();
String imagePath=getImagePath(uri, null);
displayImage(imagePath);
}
private String getImagePath(Uri uri,String selection) {
// TODO Auto-generated method stub
String path=null;
Cursor cursor=getContentResolver().query(uri, null, selection, null, null);
if (cursor!=null) {
if (cursor.moveToFirst()) {
path=cursor.getString(cursor.getColumnIndex(Media.DATA));
}
cursor.close();
}
return path;
}
private void displayImage(String imagePath) {
// TODO Auto-generated method stub
if (imagePath!=null) {
Bitmap bitmap=BitmapFactory.decodeFile(imagePath);
picture.setImageBitmap(bitmap);
}else{
Toast.makeText(this, "failed to get image", 0).show();
}
}
}
3.加上权限
具体项目代码见本人博客资源