Skip to content

Commit

Permalink
fix 1059 Converting large image to pdf causes app to crash
Browse files Browse the repository at this point in the history
  • Loading branch information
deepoceansame committed Apr 23, 2022
1 parent 387931d commit 3e5f12b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/src/main/java/swati4star/createpdf/util/CreatePdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected String doInBackground(String... params) {
if (StringUtils.getInstance().isNotEmpty(mQualityString)) {
quality = Integer.parseInt(mQualityString);
}
Image image = Image.getInstance(mImagesUri.get(i));
Image image = Image.getInstance(ImageUtils.stringPathToByteArray(mImagesUri.get(i)));
// compressionLevel is a value between 0 (best speed) and 9 (best compression)
double qualityMod = quality * 0.09;
image.setCompressionLevel((int) qualityMod);
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/java/swati4star/createpdf/util/ImageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.zhihu.matisse.engine.impl.PicassoEngine;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

Expand Down Expand Up @@ -215,6 +216,30 @@ public Bitmap toGrayscale(Bitmap bmpOriginal) {
return bmpGrayscale;
}


/**
* Convert string path to ByteArray and compress image in the procedure
*
* @param path - name of the file
*/
public static byte[] stringPathToByteArray(String path) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);

int reqHeight = 3840;
int reqWidth = 2180;
options.inSampleSize = Math.max(options.outHeight / reqHeight, options.outWidth / reqWidth);
options.inSampleSize = Math.max(options.inSampleSize, 1);

options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteStream);
return byteStream.toByteArray();
}


/**
* Saves bitmap to external storage
*
Expand Down

0 comments on commit 3e5f12b

Please sign in to comment.