android – 如何优化像Facebook和WhatApp这样的图像?

我想优化图像文件的大小,像whatsapp和Facebook正在做的.我已经发送了5MB的图片,whatsapp,并且收到的图像大小为80KB.收到的图像与原始图像相似,但分辨率小于原始图像.

我尝试几乎所有的源代码android图像压缩可用在stackoverflow,但不适用于我.然后我碰到this link来优化图像,这是做好的工作,但仍然没有得到像whatsapp的结果.

如何在不降低图像质量的情况下实现最大图像压缩,就像whatsapp所做的那样?

用源代码回答将是非常有帮助的.

提前致谢.

解决方法

你需要解码图像(位图)

这是代码.

public Bitmap  decodeFile(String path) {
        // Decode image size
        int orientation;
        try {
            if (path == null) {
                return null ;
            }
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            // Find the correct scale value. It should be the power of 2.
            final int required_SIZE = 70;
            int width_tmp = o.outWidth,height_tmp = o.outHeight;
            int scale = 0;
            while (true) {
                if (width_tmp / 2 < required_SIZE
                        || height_tmp / 2 < required_SIZE)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale++;
            }
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            Bitmap bm = BitmapFactory.decodeFile(path,o2);
            Bitmap bitmap = bm;
            ExifInterface exif = new ExifInterface(path);
            orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,1);

            Log.e("orientation","" + orientation);
            bitmap = Bitmap.createBitmap(bm,bm.getWidth(),bm.getHeight(),null,true);
            ImageViewChooseImage.setimageBitmap(bitmap);
            bitmapfinal = bitmap;
            return bitmap ;
        } catch (Exception e) {
            e.printstacktrace();
            return null;
        }

    }

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...