我在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
? –