通过拍照或相册中获取图片,并进行裁剪操作,然后把图片显示到ImageView上。
当然也可以上传到服务器(项目中绝大部分情况是上传到服务器),参考网上资料及结合项目实际情况,
测试了多款手机暂时没有发现严重问题。代码有注释,直接贴代码:
public class UploadPicActivity extends Activity implements View.OnClickListener {
private Button take_photo_btn;
private Button select_photo_btn;
private ImageView photo_iv;
//使用照相机拍照获取图片
public static final int TAKE_PHOTO_CODE = 1;
//使用相册中的图片
public static final int SELECT_PIC_CODE = 2;
//图片裁剪
private static final int PHOTO_CROP_CODE = 3;
//定义图片的Uri
private Uri photoUri;
//图片文件路径
private String picPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_pic);
initViews();
}
private void initViews() {
this.take_photo_btn = (Button) findViewById(R.id.take_photo_btn);
this.take_photo_btn.setOnClickListener(this);
this.select_photo_btn = (Button) findViewById(R.id.select_photo_btn);
this.select_photo_btn.setOnClickListener(this);
this.photo_iv = (ImageView) findViewById(R.id.photo_iv);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
//拍照
case R.id.take_photo_btn:
picTyTakePhoto();
break;
//选择图库
case R.id.select_photo_btn:
pickPhoto();
break;
}
}
/**
* 拍照获取图片
*/
private void picTyTakePhoto() {
//判断SD卡是否存在
String SDState = Environment.getExternalStorageState();
if (SDState.equals(Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//"android.media.action.IMAGE_CAPTURE"
/***
* 使用照相机拍照,拍照后的图片会存放在相册中。使用这种方式好处就是:获取的图片是拍照后的原图,
* 如果不实用ContentValues存放照片路径的话,拍照后获取的图片为缩略图有可能不清晰
*/
ContentValues values = new ContentValues();
photoUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, TAKE_PHOTO_CODE);
} else {
Toast.makeText(this, "内存卡不存在", Toast.LENGTH_LONG).show();
}
}
/***
* 从相册中取图片
*/
private void pickPhoto() {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(intent, SELECT_PIC_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
//从相册取图片,有些手机有异常情况,请注意
if (requestCode == SELECT_PIC_CODE) {
if (null != data && null != data.getData()) {
photoUri = data.getData();
picPath = uriToFilePath(photoUri);
startPhotoZoom(photoUri, PHOTO_CROP_CODE);
} else {
Toast.makeText(this, "图片选择失败", Toast.LENGTH_LONG).show();
}
} else if (requestCode == TAKE_PHOTO_CODE) {
String[] pojo = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(photoUri, pojo, null, null, null);
if (cursor != null) {
int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
cursor.moveToFirst();
picPath = cursor.getString(columnIndex);
if (Build.VERSION.SDK_INT < 14) {
cursor.close();
}
}
if (picPath != null) {
photoUri = Uri.fromFile(new File(picPath));
startPhotoZoom(photoUri, PHOTO_CROP_CODE);
} else {
Toast.makeText(this, "图片选择失败", Toast.LENGTH_LONG).show();
}
} else if (requestCode == PHOTO_CROP_CODE) {
if (photoUri != null) {
Bitmap bitmap = BitmapFactory.decodeFile(picPath);
if (bitmap != null) {
//这里可以把图片进行上传到服务器操作
photo_iv.setImageBitmap(bitmap);
}
}
}
}
}
/**
* @param
* @description 裁剪图片
* @author ldm
* @time 2016/11/30 15:19
*/
private void startPhotoZoom(Uri uri, int REQUE_CODE_CROP) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
intent.putExtra("crop", "true");
// 去黑边
intent.putExtra("scale", true);
intent.putExtra("scaleUpIfNeeded", true);
// aspectX aspectY 是宽高的比例,根据自己情况修改
intent.putExtra("aspectX", 3);
intent.putExtra("aspectY", 2);
// outputX outputY 是裁剪图片宽高像素
intent.putExtra("outputX", 600);
intent.putExtra("outputY", 400);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
//取消人脸识别功能
intent.putExtra("noFaceDetection", true);
//设置返回的uri
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//设置为不返回数据
intent.putExtra("return-data", false);
startActivityForResult(intent, REQUE_CODE_CROP);
}
/**
* @param
* @description 把Uri转换为文件路径
* @author ldm
* @time 2016/11/30 15:22
*/
private String uriToFilePath(Uri uri) {
//获取图片数据
String[] proj = {MediaStore.Images.Media.DATA};
//查询
Cursor cursor = managedQuery(uri, proj, null, null, null);
//获得用户选择的图片的索引值
int image_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
//返回图片路径
return cursor.getString(image_index);
}
}
布局文件长这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<Button
android:id="@+id/take_photo_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="拍照"
android:textSize="16sp"/>
<Button
android:id="@+id/select_photo_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="选择图片"
android:textSize="16sp"/>
<ImageView
android:id="@+id/photo_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"/>
</LinearLayout>
最后不要忘记在AndroidManifest.xml中添加UploadPicActivity及权限:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>