【发布时间】:2017-03-29 03:00:31
【问题描述】:
我在 sd 卡上有一个大图像文件,我想在上传到服务器之前对其进行压缩。但是要使用Bitmap.compress(CompressFormat format, int quality, OutputStream stream)压缩图像,我们需要将图像读入内存并保存压缩版本。
有没有办法在不将位图读入内存的情况下生成图像的压缩版本。
我正在使用以下代码进行压缩。
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap finalBitmap = BitmapFactory.decodeFile(originalFile.getAbsolutePath(), options);
File local = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "/my_images");
local.mkdirs();
File compressedFile = new File(local, "test.jpg");
FileOutputStream out = new FileOutputStream(compressedFile);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 70, out);
【问题讨论】:
-
将位图保存到磁盘并压缩文件。
-
图像已保存在磁盘上,大小约为 100MB。所以我不想将位图加载到内存中,因为它会导致 OOM。我需要的是创建一个压缩版本的文件,以优化上传时间。
-
您以哪种格式上传图片?
-
首先在设备上压缩图像有什么价值吗?我的意思是把它上传到服务器并在需要时压缩它——它肯定会有非常优越的资源吗?
-
100mb?图片的分辨率是多少?
标签: android android-bitmap image-compression