如何在上传到服务器之前减小图像文件大小 [英] How to reduce an Image file size before uploading to a server

查看:23
本文介绍了如何在上传到服务器之前减小图像文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多应用程序允许共享从图库中挑选的图像.

Lot of application allow sharing an image, which is picked from the gallery.

他们会上传原始图像文件吗?哪个是 1-3 mb?还是他们处理?

Do they upload the original image file? Which is like 1-3 mb? Or do they process?

无论如何,我如何从文件路径中获取图像,通过降低分辨率来减小其大小并将其保存在其他位置并尝试上传?

In any case, how can I take the image from a filepath, reduce its size by lowering the resolution and save it some where else and the try to upload?

我试过了:

Bitmap photo = decodeSampledBitmapFromFile(filePath, DESIRED_WIDTH,
                    DESIRED_HEIGHT);

FileOutputStream out = new FileOutputStream(filePath);
photo.compress(Bitmap.CompressFormat.JPEG, 100, out);

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth,
        int reqHeight) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    final int height = options.outHeight;
    final int width = options.outWidth;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    int inSampleSize = 1;

    if (height > reqHeight) {
        inSampleSize = Math.round((float) height / (float) reqHeight);
    }
    int expectedWidth = width / inSampleSize;

    if (expectedWidth > reqWidth) {
        inSampleSize = Math.round((float) width / (float) reqWidth);
    }
    options.inSampleSize = inSampleSize;
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(path, options);
}

但这是他们正确的做法吗?因为我看到的答案表明压缩操作需要相当多的时间 here

But is this they right way to do? Because I have seen answers suggesting compression operation takes rather big amount of time here

推荐答案

我用这个功能在上传图片之前缩小图片大小,将图片缩小到近200 KB,并保持质量相对较好,你可以修改它通过更改 REQUIRED_SIZE 和 inSampleSize 来实现您的目的:

I use this function to reduce the size of image before uploading it, it reduces the image size to nearly 200 KB and keep the quality relatively good, u may modify it to fulfill your purpose by changing the REQUIRED_SIZE and inSampleSize:

public File saveBitmapToFile(File file){
    try {

        // BitmapFactory options to downsize the image
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        o.inSampleSize = 6;
        // factor of downsizing the image

        FileInputStream inputStream = new FileInputStream(file);
        //Bitmap selectedBitmap = null;
        BitmapFactory.decodeStream(inputStream, null, o);
        inputStream.close();

        // The new size we want to scale to
        final int REQUIRED_SIZE=75;

        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while(o.outWidth / scale / 2 >= REQUIRED_SIZE &&
                        o.outHeight / scale / 2 >= REQUIRED_SIZE) {
            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        inputStream = new FileInputStream(file);

        Bitmap selectedBitmap = BitmapFactory.decodeStream(inputStream, null, o2);
        inputStream.close();

        // here i override the original image file
        file.createNewFile();
        FileOutputStream outputStream = new FileOutputStream(file);

        selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100 , outputStream);

        return file;
    } catch (Exception e) {
        return null;
    }
}

注意:我在此函数中调整大小并覆盖原始文件图像,您也可以将其写入另一个文件.

NOTE: I resize and override the original file image in this function, u may write it to another file as well.

希望对你有帮助.

这篇关于如何在上传到服务器之前减小图像文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆