选择图片
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent,REQUEST_GET_CONTENT_CODE);
获取图片的uri
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) {
return;
}
if (requestCode == 0) {
Uri uri = data.getData();
String realPathFromURI = Utils.getRealPathFromURI(this, uri);
}
}
根据uri获取图片的真实路径
public static String getRealPathFromURI(Activity context, Uri uri) {
String imagePath = null;
String docId = DocumentsContract.getDocumentId(uri);
String id = docId.split(":")[1];
String selection = MediaStore.Images.Media._ID+"="+id;
Uri externalContentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = context.getContentResolver().query(externalContentUri,
null,
selection,
null,
null);
if(cursor!=null){
if(cursor.moveToFirst()){
imagePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
}
return imagePath;
}