private String userUri=null; //从相册中选择照片 public static final int CHOOSE_PHOTO = 2; private void openAlbum() { //action_get_content是通过intent中设置的type属性来判断具体调用哪个程序的 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(intent, CHOOSE_PHOTO); } @TargetApi(19) private String handleImageOnKitKat(Intent data) { String imagePath = null; Uri uri = data.getData(); Log.d(TAG, "handleImageOnKitKat:获取的 uri=data.getData() "+uri); if (DocumentsContract.isDocumentUri(this, uri)) { //如果是document 类型的Uri ,则通过document_id 来处理 String docId = DocumentsContract.getDocumentId(uri); if ("com.android.providers.media.documents".equals(uri.getAuthority())) { String id = docId.split(":")[1];//解析出数据格式的ID String selection = MediaStore.Images.Media._ID + "=" + id; imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection); Log.d(TAG, "com.android.providers.media.documents: "); } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) { Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId)); imagePath = getImagePath(contentUri, null); Log.d(TAG, "com.android.providers.downloads.documents: "); } } else if ("content".equalsIgnoreCase(uri.getScheme())) { //如果是普通类型的Uri,则使用普通的方式来处理 imagePath = getImagePath(uri, null); // imagePath = getRealPathFromURI(uri); Log.d(TAG, "content: "); } else if ("file".equalsIgnoreCase(uri.getScheme())) { //如果是file类型的uri,直接获取图片路径就可以了 imagePath = uri.getPath(); Log.d(TAG, "file "); } Log.d(TAG, "handleImageOnKitKat:1 图片真实路径imagePath " + imagePath); return imagePath; } //android 4.4一下就直接调用这个方法就可以了 private String handleImageBeforeKitKat(Intent data) { Uri uri = data.getData(); String imagePath = getImagePath(uri, null); Log.d(TAG, "handleImageBeforeKitKat:2 图片真实路径imagePath " + imagePath); return imagePath; } //根据uri获取路径 private String getImagePath(Uri uri, String selection) { String path = null; //通过Uri 和selection来获取真实的图片路径 Cursor cursor = 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; } private String getRealPathFromURI( Uri contentUri) { Cursor cursor = null; try { String[] proj = { MediaStore.Images.Media.DATA }; cursor = getContentResolver().query(contentUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String path = cursor.getString(column_index); return path; } finally { if (cursor != null) { cursor.close(); } } }@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case CHOOSE_PHOTO: if (resultCode == RESULT_OK) { if (Build.VERSION.SDK_INT >= 19) { userUri=handleImageOnKitKat(data); } else { userUri=handleImageBeforeKitKat(data); } } break; } }
注意:android 6.0要添加sd卡的动态权限
android 4.4以上从相册进入之后,获取图片的路径的方法
最新推荐文章于 2022-11-18 11:10:52 发布