安卓拍照问题解决

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaoyu_93/article/details/63683621

一、部分安卓手机拍照后,调用拍照的activity被重建

    在部分手机上会出现拍照回来后,界面上东西还原了的问题(比如输入框内的输入拍照回来后没有了)。后来打log查看生命周期的时候发现activity被重建了。为了防止该问题的出现,需要做如下处理:

  • 在AndroidMainifest.xml中对应的调用拍照的activity中添加两个属性:

android:configChanges=”orientation|keyboardHidden”
android:launchMode=”singleTask”

  • 在调用拍照的activity中java代码内,复写方法onConfigurationChanged,其实这里什么特殊操作也没做。如果项目中所有activity有基类,则在基类中加这个代码就行了。
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

二、部分安卓手机中图片文件获取bitmap时旋转了90度

    在我的魅族手机拍照后根据图片路径获取bitmap时,发现bitmap图像旋转了90度,部分手机会存在这个问题(可能是手机在拍照保存的时候的问题)。所以需要在根据文件路径获取bitmap对象时判断该文件的旋转角度,若有旋转,则获取到bitmap后对bitmap进行旋转以还原(在手机相册查看图片的时候也是正的,莫非手机相册程序做了这个处理?)。如下工具类中的getSmallBitmap方法,可针对图片文件路径,获取一个压缩后(防止OOM,而且一般需求也不需要获取高清大图)的且一定是正常显示没进行旋转的bitmap对象。

public class BitmapUtils {
    /**
     * 根据路径获得图片并压缩,返回bitmap用于显示
     * 用于显示本地图片时使用
     *
     * @param filePath 图片路径
     * @param width    压缩后的宽
     * @param height   压缩后的高
     * @return
     */
    public static Bitmap getSmallBitmap(String filePath, int width, int height) {
        int orientation = readPictureDegree(filePath);//获取旋转角度

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, width, height);
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);

        if (Math.abs(orientation) > 0) {
            bitmap = BitmapUtils.rotaingImageView(orientation, bitmap);//旋转图片
        }

        return bitmap;
    }

    // 读取图片文件的旋转角度
    private static int readPictureDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

    // 将bitmap旋转指定角度
    private static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
        //旋转图片 动作
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        // 创建新的图片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return resizedBitmap;
    }

    /**
     * 计算图片的缩放值,等比例缩放使得图片宽高不超过指定宽高
     *
     * @param options
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }

}

猜你喜欢

转载自blog.csdn.net/xiaoyu_93/article/details/63683621
今日推荐