我在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。我需要的是创建一个压缩版本的文件,以优化上传时间。你上传图片的格式是 – Ammar
? –