踩坑:图片拍照上传等
原创
©著作权归作者所有:来自51CTO博客作者钰娘娘ynn的原创作品,请联系作者获取转载授权,否则将追究法律责任
以前这个功能也做过,无奈这次碰上的坑特别多,特此记录。
为啥拍照后总resultCode总是0?
检查了许久,发现,存储的位置不在sdcard,所以拍照后的resultCode总是0。以前没有关注过,以后就要注意,拍照后存储的位置必然sdcard,否则拍照后的结果为失败。
空图片,一选就挂掉
这一问题主要源于上面,上面创建了图片文件,但因为没有正确放入图片,所以大小为空,所以压缩之前要判断是否为空。
部分手机的图片旋转问题
有些手机拍照后图片会发生旋转,这个时候查看也会发现是旋转的,此时需要用特定方法旋转回正确的位置。
public static Bitmap rotateBitmap(Bitmap b, String filepath) {
int degrees = getExifOrientation(filepath);
if(degrees != 0 && b != null) {
Matrix m = new Matrix();
m.setRotate((float)degrees, (float)b.getWidth() / 2.0F, (float)b.getHeight() / 2.0F);
try {
Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
if(b != b2) {
b.recycle();
b = b2;
}
} catch (OutOfMemoryError var5) {
;
}
}
return b;
}
public static int getExifOrientation(String filepath) {
short degree = 0;
ExifInterface exif = null;
try {
exif = new ExifInterface(filepath);
} catch (IOException var4) {
;
}
if(exif != null) {
int orientation = exif.getAttributeInt("Orientation", -1);
if(orientation != -1) {
switch(orientation) {
case 3:
degree = 180;
case 4:
case 5:
case 7:
default:
break;
case 6:
degree = 90;
break;
case 8:
degree = 270;
}
}
}
return