版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34206863/article/details/88422568
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (Build.VERSION.SDK_INT >= 19) {
handlePathOnKitKat(data);
} else {
handlePathBeforeKitKat(data);
}
}
}
private void handlePathBeforeKitKat(Intent data) {
Uri uri = data.getData();
String path = uri.getPath();
//somethings
}
@TargetApi(19)
private void handlePathOnKitKat(Intent data) {
String path = null;
Uri uri = data.getData();
if (DocumentsContract.isDocumentUri(getActivity(), uri)) {
String docId = DocumentsContract.getDocumentId(uri);
if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
String id = docId.split(":")[1];
String selection = MediaStore.Images.Media._ID + "=" + id;
path = getPath(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));
path = getPath(contentUri, null);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
path = getPath(uri, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
path = uri.getPath();
}
//somethins
}
private String getPath(Uri uri, String selection) {
String path = null;
Cursor cursor = App.getContext().getContentResolver().query(uri, null, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();
}
return path;
}
记录一下,免得到处找
ps: not by me! its god of Guo