解决华为手机选择图片路径不一样问题

从图库里选择图片是我们开发中非常常用的功能,如上传头像,上传相册。

Android4.4之前的版本打开图库选择图片可以这样:

<span style="background-color: rgb(255, 255, 255);">Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
activity.startActivityForResult(intent, START_ALBUM_CODE);</span>
<span style="background-color: rgb(255, 255, 255);">Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
activity.startActivityForResult(intent, START_ALBUM_CODE);

这样就可以打开图库,</span><span style="background-color: rgb(255, 255, 255);"><font style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; display: inline ! important; float: none;" color="#000000" size="2" face="Arial, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif">然后我们重写onActivityResult(),在返回的data里我们就能获取图片的路径。
<font face="Arial, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif">打印一下选择<font face="Arial, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif">图片的路径是:<span style="background-color: rgb(255, 255, 255);"><font style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; display: inline ! important; float: none;" color="#000000" size="2" face="Arial, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif">/document/image%3A29817
<font face="Arial, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif"><font face="Arial, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif">而我们正常的路径是:<span style="background-color: rgb(255, 255, 255);"><span style="font-family:Arial, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif;font-size:12px;color:#000000;font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; display: inline ! important; float: none;">/mnt/sdcard/DCIM/100MEDIA/IMAG0021.jpg</span></span>
<font face="Arial, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif">这里的路径是不一样的,原因是华为手机有个云存储的,他存储的是一个<font face="Arial, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif">图片的云地址,<font face="Arial, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif">而不是一个真实地址,所以打你选中的时候,不会<font face="Arial, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif">返回数据
<span style="font-family:Arial, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif;">你会发现<span style="font-family:Arial, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif;">data.getData()<span style="font-family:Arial, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif;"> == null。</span></span></span><br style="color: rgb(0, 0, 0); font-family: Arial,Helvetica,"Hiragino Sans GB",微软雅黑,"Microsoft YaHei UI",SimSun,SimHei,arial,sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;" /></font></font></font></font></font></font></font></span></font></font></font></span><span style="background-color: rgb(255, 255, 255);"><span style="font-size:12px;"><span style="font-family:Arial, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif;"><span style="font-family:Arial, Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif;">现在只需要改一个代码就行,</span>
</span></span></span>
在Android4.4上有效的方法应该是这样(也兼容4.4以前的版本):
<span style="background-color: rgb(255, 255, 255);">Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
activity.startActivityForResult(intent, START_ALBUM_CODE);</span>

这样就能获取正确的path.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值