android 4.4以上从相册进入之后,获取图片的路径的方法

  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卡的动态权限




































 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值