美文网首页
Android 华为手机获取相册图片路径,获取不到问题

Android 华为手机获取相册图片路径,获取不到问题

作者: 程序员K哥 | 来源:发表于2018-03-22 16:06 被阅读660次

    未经本人授权,不得转载!否则必将维权到底

    有个需求,可以从系统相册选择图片,上传到服务器。那么选择从系统相册选择完图片后,图片的名字需要显示在页面上。这里出了个 Bug,华为手机用系统的方法,获取相册图片路径,返回 null。用别的品牌的手机,却又是正常的。

    问题展示:

    华为 Mate10.jpg

    解决步骤:

    1、Debug 跟了一遍代码,发现代码里面获取相册图片的路径为 null,因为做了非空判断,所以直接返回 null 了。
    2、Google ,发生此 bug 的根本原因是版本不同导致的 Uri 的问题。Android 4.3 及以下,根据 Uri 来查询系统相册,得到照片的 path 完全没有问题。而 Android 4.4 返回的 Uri 跟 Android 4.3 及以下完全不一样。Android 4.3 返回的是文件路径,而 Android 4.4 返回的却是“content://com.android.providers.media.documents/document/image:xxxxx”,拿这个 Uri 去查询系统相册,图片路径返回肯定为 null 了。

    可行方案:通过下面的方法将 Uri 转换成我们需要的 path 即可

        public static String getImagePathFromURI(Activity activity,Uri uri) {
            Cursor cursor = activity.getContentResolver().query(uri, null, null, null, null);
            String path = null;
            if (cursor != null) {
            cursor.moveToFirst();
            String document_id = cursor.getString(0);
            document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
            cursor.close();
    
                    cursor = activity.getContentResolver().query(
                                                                      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                                                      null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
                    if (cursor != null) {
                        cursor.moveToFirst();
                        path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                        cursor.close();
                    }
                }
                return path;
            }
    

    逻辑完整代码:

        /**
         * @param uri
         * @param multipartContent
         * @return
         * @throws Exception 
         */
        public static String uploadImageInWeb(String uploadKey,Uri uri,Activity context, CustomMultipartEntity multipartContent) throws Exception {
            String[] proj = { MediaStore.Images.Media.DATA };
            Cursor actualimagecursor = context.managedQuery(uri,proj,null,null,null);
            int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            actualimagecursor.moveToFirst();
            String  img_path = "";
            //如果是华为手机,调用上面给出的方法,即可得到正确的图片途径
            if(isHuaWeiPhone()){
                img_path = getImagePathFromURI(context,uri);
            }else{
                img_path = actualimagecursor.getString(actual_image_column_index);
            }
            
            if (img_path==null) {
                return null;
            }
            File imageFile = new File(img_path);
            KeithXiaoYClient client = KeithXiaoYApplication.mClient;
            String url = Constants.serverAdd
                    + "?sessionkey="
                    + Constants.sessionKey;
            LogUtils.i("keithxiaoy", "上传的网址是----->" + url + "/");
            Map<String, String> params = new HashMap<String, String>();
            params.put("uploadFileName", imageFile.getName());
            params.put("uploadContentType", "image/png");
            params.put("uploadKey", uploadKey);
            params.put("method", "upload");
            multipartContent.addPart("uploadFile", new FileBody(imageFile));
            JSONObject object = client.uploadMediaFiles(url, params,multipartContent);
            JSONArray jsonArray = object.getJSONArray("upload");
            if (jsonArray != null) {
                LogUtils.i("keithxiaoy", "上传成功");
            } else {
                LogUtils.i("keithxiaoy", "上传失败");
            }
            return imageFile.getName();
        }
    

    解决后的效果展示:

    图例.jpg
    不要给自己的人生设限

    相关文章

      网友评论

          本文标题:Android 华为手机获取相册图片路径,获取不到问题

          本文链接:https://www.haomeiwen.com/subject/yilbxftx.html