I'm trying to compress an image that I saved in the file. I'm trying to compress the File into 1MB. I try a few way but it usually make an OutofMemoryError. and then i tried to use this solution, but it makes the bitmap blank.
How to compress bitmap from 10mb image from camera to 300kb beforw setting to imageview in android
Here is my code :
System.gc();
getActivity().getContentResolver().notifyChange(mImageTempUri, null);
Bitmap bitmap;
bitmap = BitmapFactory.decodeFile(mImageDirectory + mImageName, options);
if(bitmap == null){
howRequestFailedErrorMessage("Gambar gagal di-upload");
return;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 25, bytes);
File f = new File(mImageDirectory + mImageName);
if(f.exists()){
f.delete();
}
FileOutputStream fo;
try {
fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.flush();
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
bitmap.recycle();