您的位置:首页 > 其它

同事写的图片压缩方法

2013-12-20 10:21 330 查看
if (MessageUtils.ensureImageSizeExceed(this, imgpath)) {

            imgpath = MessageUtils.compressPicture(this, imgpath);

 }

public static boolean ensureImageSizeExceed (Context context, String filePath) {

        if (TextUtils.isEmpty(filePath)) {

            return false;

        }

        File file = new File(filePath);

        long fileSize = file.length();

        // when size exceed 1M

        if (fileSize > IMAGE_LIMIT) {

            return true;

        }

        return false;

    }

    public static final int IMAGE_LIMIT = 300 * 1024;

    /**

     * @param filePath the file path need compress

     * @return the compressed file path

     * */

    public static String compressImage(Context context, String filePath) {

        long time = System.currentTimeMillis();

        Uri fileUri = Uri.fromFile(new File(filePath));

        InputStream input = null;

        int imageWidth = 480;

        int imageHeight = 800;

        try {

            input = context.getContentResolver().openInputStream(fileUri);

            Log.i(TAG, "liuqiang123--before file size="+new File(filePath).length());

            BitmapFactory.Options opt = new BitmapFactory.Options();

            opt.inJustDecodeBounds = true;

            BitmapFactory.decodeStream(input, null, opt);

            imageWidth = opt.outWidth;

            imageHeight = opt.outHeight;

            if (input != null) {

                input.close();

            }

            int widthLimit = MmsConfig.getMaxImageWidth();

            int heightLimit = MmsConfig.getMaxImageHeight();

            if (imageHeight > imageWidth) {

                int temp = widthLimit;

                widthLimit = heightLimit;

                heightLimit = temp;

            }

            int limitSize = MmsConfig.getMaxMessageSize() * 1024;

            if (limitSize <= 0) {

                limitSize = IMAGE_LIMIT;

            }

            long time1 = System.currentTimeMillis();

            byte[] data = UriImage.getResizedImageData(imageWidth, imageHeight, widthLimit,

                    heightLimit, limitSize, fileUri, context);

            Log.i(TAG, "liuqiang123--getResizedImageData pay time="+(System.currentTimeMillis() - time1));

            String path = pathForSendFile(context, generateFileName(PICTURE), PICTURE);

            FileOutputStream fout = new FileOutputStream(path);

            fout.write(data);

            fout.close();

            Log.i(TAG, "liuqiang123--after file size="+new File(path).length());

            Log.i(TAG, "liuqiang123--compressImage() pat time=" + (System.currentTimeMillis() - time));

            return path;

        } catch (FileNotFoundException e) {

            Log.e(TAG, "liuqiang123--compressImage()--FileNotFoundException", e);

        } catch (IOException e) {

            Log.e(TAG, "liuqiang123--compressImage()--IOException", e);

        }

        return null;

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