开发都会遇到从手机的相册和照相机选择图片的功能,下面是一段通用的代码,用来选择手机中的图片,以后用的时候,直接调用就可以了。
使用到的类属性
public final static int CONSULT_DOC_PICTURE = 1000;
public final static int CONSULT_DOC_CAMERA = 1001;
private int SELECT_PICTURE = 0;
private int SELECT_CAMERA = 1;
private Bitmap bmp;
private Uri outputFileUri;
//从相册或照相机选择出的文件,可以用来判断是否上传
File selectImg;
弹出选择对话框
protected void createSelectImageDialog() {
CharSequence[] items = { "相册", "照相机" };
new AlertDialog.Builder(this).setTitle("选择图片来源").setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == SELECT_PICTURE) {
Intent intent = new Intent();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
intent.setAction(Intent.ACTION_GET_CONTENT);
}
else {
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
}
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "选择图片"), CONSULT_DOC_PICTURE);
} else {
File file = new File(Environment.getExternalStorageDirectory(), "avator.jpg");
outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CONSULT_DOC_CAMERA);
}
}
}).create().show();
}
获取返回的数据,因为使用的不是return-data,所以返回的是uri需要自己去查询一下真实路径
@SuppressLint("NewApi")
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CONSULT_DOC_PICTURE) {
if(data == null){
return;
}
if (bmp != null)// 如果不释放的话,不断取图片,将会内存不够
bmp.recycle();
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT){
Uri contentUri = data.getData();
String wholeID = DocumentsContract.getDocumentId(contentUri);
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column,
sel, new String[] { id }, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
String path = cursor.getString(columnIndex);
bmp = BitmapFactory.decodeFile(path);
avator.setImageBitmap(bmp);
selectImg = new File(path);
}
cursor.close();
}else{
Uri uri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = this.getContentResolver().query(uri, proj, // Which
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(column_index);
bmp = BitmapFactory.decodeFile(path);
avator.setImageBitmap(bmp);
selectImg = new File(path);
cursor.close();
}
} else if (requestCode == CONSULT_DOC_CAMERA) {
bmp = BitmapFactory.decodeFile(outputFileUri.getPath());
avator.setImageBitmap(bmp);
selectImg = new File(outputFileUri.getPath());
} else {
Toast.makeText(this, "请重新选择图片", Toast.LENGTH_SHORT).show();
}
}
调用的时候只需要在触发事件中 直接调用createSelectImageDialog方法
选择出来的图片是原始图片,图片很大,不适于上传功能