您的位置:首页 > 移动开发 > Android开发

android照相、相册获取图片剪裁报错的解决方法

2015-01-12 14:47 495 查看
最近在项目中用到了照相和相册取图剪裁上传头像,就在网上逛了逛,基本都是千篇一律,就弄下来用了用,没想到的是各种各样的奇葩问题就出现了。先给大家看看代码问题慢慢来解决

这是调用相机

在这里我返回了一个file对象,这是应为项目中需要,大家可以不必真写,直接传一个Uri对象过来就好了

下面是调用相册

?
当然接下来是调用Activity的OnActivityResult了

?
当然还有大家关心的剪切

?
在很多博客中都把“return-data”设置为了true然后在onActivityResult中通过data.getParcelableExtra("data")来获取数据,不过这样的话dp这个变量的值就不能太大了,不然你的程序就挂了。这里也就是我遇到问题的地方了,在大多数高配手机上这样用是没有问题的,不过很多低配手机就有点hold不住了,直接就异常了,包括我们的国产神机米3也没能hold住,所以我建议大家不要通过return data 大数据,小数据还是没有问题的,说以我们在剪切图片的时候就尽量使用Uri这个东东来帮助我们。

下面是我们进行剪裁用到的一些参数

Exta Options Table for image/* crop:

SetExtraDataTypeDescription
cropStringSignals the crop feature
aspectXintAspect Ratio
aspectYintAspect Ratio
outputXintwidth of output created from this Intent
outputYintwidth of output created from this Intent
scalebooleanshould it scale
return-databooleanReturn the bitmap with Action=inline-data by using the data
dataParcelableBitmap to process, you may provide it a bitmap (not tested)
circleCropStringif this string is not null, it will provide some circular cr
MediaStore.EXTRA_OUTPUT ("output")URISet this URi to a File:///, see example code
最后把通过Uri获得bitmap的方法给大家贴上

?
?
当然接下来是调用Activity的OnActivityResult了

?
当然还有大家关心的剪切

?
在很多博客中都把“return-data”设置为了true然后在onActivityResult中通过data.getParcelableExtra("data")来获取数据,不过这样的话dp这个变量的值就不能太大了,不然你的程序就挂了。这里也就是我遇到问题的地方了,在大多数高配手机上这样用是没有问题的,不过很多低配手机就有点hold不住了,直接就异常了,包括我们的国产神机米3也没能hold住,所以我建议大家不要通过return data 大数据,小数据还是没有问题的,说以我们在剪切图片的时候就尽量使用Uri这个东东来帮助我们。

下面是我们进行剪裁用到的一些参数

Exta Options Table for image/* crop:

SetExtraDataTypeDescription
cropStringSignals the crop feature
aspectXintAspect Ratio
aspectYintAspect Ratio
outputXintwidth of output created from this Intent
outputYintwidth of output created from this Intent
scalebooleanshould it scale
return-databooleanReturn the bitmap with Action=inline-data by using the data
dataParcelableBitmap to process, you may provide it a bitmap (not tested)
circleCropStringif this string is not null, it will provide some circular cr
MediaStore.EXTRA_OUTPUT ("output")URISet this URi to a File:///, see example code
最后把通过Uri获得bitmap的方法给大家贴上

?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: