android4.4.4 获取系统相册图片出错的问题(一)

关于Android4.4的图片路径获取,回来的Uri的格式有两种:

content://com.android.providers.media.documents/document/image:3951

content://media/external/images/media/3951

网上有很多个版本,但是自己后来写了个版本兼容4.4以下,其实原理就是当该系统是4.4或以上的时候,我们在SD卡中的ImgCache文件夹重新生成了该图片,然后在获取这张图片的路径返回,性能还是有待测试:

/**
     * @方法说明:获得相册图片的路径
     * @方法名称:getAblumPicPath
     * @param data
     * @param ac
     * @return
     * @返回 String
     */
    public static String getAblumPicPath(Intent data, Activity ac) {
        Uri originalUri = data.getData();
        String[] proj = { MediaStore.Images.Media.DATA };
        if (originalUri != null && proj != null) {
            Cursor cursor = ac.getContentResolver().query(originalUri, null,
                    null, null, null);
            if (cursor == null) {
                String path = originalUri.getPath();
                if (!Utils.isEmpty(path)) {
                    String type = ".jpg";
                    String type1 = ".png";
                    if (path.endsWith(type) || path.endsWith(type1)) {
                        return path;
                    } else {
                        return "";
                    }
                } else {
                    return "";
                }
            } else
                /**将光标移至开,这个很重要,不小心很容易引起越**/
                cursor.moveToFirst();
            /**按我个人理解 这个是获得用户选择的图片的索引**/
            int column_index = cursor.getColumnIndex(proj[0]);
            /** 最后根据索引值获取图片路**/
            String path = cursor.getString(column_index);
            cursor.close();
            return path;
        } else {
            Bundle bundle = data.getExtras();
            String path = "";
            if (bundle != null) {
                Bitmap photo = (Bitmap) bundle.get("data");
                try {
                    File file = Utils.creatFilePath(Utils.getFilePath()
                            + "/ImgCache/", Utils.getCutimeIsShow()
                            + ".jpg");
                    path = file.getAbsolutePath();
                    BufferedOutputStream bos = new BufferedOutputStream(
                            new FileOutputStream(path, false));
                    photo.compress(Bitmap.CompressFormat.JPEG, 100, bos);
                    bos.flush();
                    bos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return path;
        }
    }

当我们在android 4.4或以上系统中系统相册中选取照片的时候,我们只要在调用的类中的


@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch(requestCode){
            case ablum:
                if (resultCode != Activity.RESULT_OK) {
                    return;
                }
                /**获取到相册的图片的路径**/   
                String pic = getAblumPicPath(data, context);

            break;
        }
    }

最后,顺带说下,如何从系统相册中选取:

public static void picSelect(Activity ac, int ablum) {
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        // Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        ac.startActivityForResult(intent, ablum);
    }

这里把上面用的几个方法给补全下,不好意思:

/**
     * @方法说明:Test whether the SD card presence
     * @方法名称:hasSdcard
     * @return
     * @返回 boolean
     */
    public static boolean hasSdcard() {
        String status = Environment.getExternalStorageState();
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @方法说明:get the file's path
     * @方法名称:getFilePath
     * @return
     * @返回 String
     */
    public static String getFilePath() {
        if (hasSdcard()) {
            return sdPath;
        } else {
            return dataPath;
        }
    }

    /**
     * @方法说明:获得当前系统的时间
     * @方法名称:getCutimeIsShow
     * @return
     * @返回 String
     */
    public static String getCutimeIsShow() {
        return String.valueOf(System.currentTimeMillis());
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值