调用系统图库:
Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE_IMAGE);
获取图片路径
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_IMAGE && resultCode == RESULT_OK) {
Uri uri = data.getData();
Cursor cursor = getContentResolver().query(uri, null, null, null,null);
if (cursor != null && cursor.moveToFirst()) {
String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
}
}
}
调用相机返回路径1:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CODE_CAMERA);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
// 获取相机返回的数据,并转换为Bitmap图片格式,这是缩略图
Bitmap bitmap = (Bitmap) bundle.get("data");
}
}
调用相机返回路径2:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//path为保存图片的路径,执行完拍照以后能保存到指定的路径下
File file = new File(path);
Uri imageUri = Uri.fromFile(file );
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, REQUEST_CODE_CAMERA);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) {
//直接取之前保存的路径
}
}