将相机中的图像文件压缩到一定大小
compress image file from camera to certain size
我正在尝试压缩保存在文件中的图像。我正在尝试将文件压缩为 1MB。我尝试了几种方法,但它通常会导致 OutofMemoryError。
然后我尝试使用这个解决方案,但它使位图空白。
How to compress bitmap from 10mb image from camera to 300kb beforw setting to imageview in android
这是我的代码:
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();
好的,我有自己的答案
File f = new File(mImageDirectory + mImageName);
if(f.exists()){
f.delete();
}
int MAX_IMAGE_SIZE = 1000 * 1024;
int streamLength = MAX_IMAGE_SIZE;
int compressQuality = 105;
ByteArrayOutputStream bmpStream = new ByteArrayOutputStream();
while (streamLength >= MAX_IMAGE_SIZE && compressQuality > 5) {
try {
bmpStream.flush();//to avoid out of memory error
bmpStream.reset();
} catch (IOException e) {
e.printStackTrace();
}
compressQuality -= 5;
bitmap.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream);
byte[] bmpPicByteArray = bmpStream.toByteArray();
streamLength = bmpPicByteArray.length;
if(BuildConfig.DEBUG) {
Log.d("test upload", "Quality: " + compressQuality);
Log.d("test upload", "Size: " + streamLength);
}
}
FileOutputStream fo;
try {
fo = new FileOutputStream(f);
fo.write(bmpStream.toByteArray());
fo.flush();
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
Kotlin Way
if (file.length() > MAX_IMAGE_SIZE) {
var streamLength = MAX_IMAGE_SIZE
var compressQuality = 105
val bmpStream = ByteArrayOutputStream()
while (streamLength >= MAX_IMAGE_SIZE && compressQuality > 5) {
bmpStream.use {
it.flush()
it.reset()
}
compressQuality -= 5
val bitmap = BitmapFactory.decodeFile(file.absolutePath, BitmapFactory.Options())
bitmap.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream)
val bmpPicByteArray = bmpStream.toByteArray()
streamLength = bmpPicByteArray.size
if (BuildConfig.DEBUG) {
Log.d("test upload", "Quality: $compressQuality")
Log.d("test upload", "Size: $streamLength")
}
}
FileOutputStream(file).use {
it.write(bmpStream.toByteArray())
}
}
Constant
companion object {
//2000 * 1024 = 2 MB
private const val MAX_IMAGE_SIZE = 2048000
}
我正在尝试压缩保存在文件中的图像。我正在尝试将文件压缩为 1MB。我尝试了几种方法,但它通常会导致 OutofMemoryError。 然后我尝试使用这个解决方案,但它使位图空白。
How to compress bitmap from 10mb image from camera to 300kb beforw setting to imageview in android
这是我的代码:
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();
好的,我有自己的答案
File f = new File(mImageDirectory + mImageName);
if(f.exists()){
f.delete();
}
int MAX_IMAGE_SIZE = 1000 * 1024;
int streamLength = MAX_IMAGE_SIZE;
int compressQuality = 105;
ByteArrayOutputStream bmpStream = new ByteArrayOutputStream();
while (streamLength >= MAX_IMAGE_SIZE && compressQuality > 5) {
try {
bmpStream.flush();//to avoid out of memory error
bmpStream.reset();
} catch (IOException e) {
e.printStackTrace();
}
compressQuality -= 5;
bitmap.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream);
byte[] bmpPicByteArray = bmpStream.toByteArray();
streamLength = bmpPicByteArray.length;
if(BuildConfig.DEBUG) {
Log.d("test upload", "Quality: " + compressQuality);
Log.d("test upload", "Size: " + streamLength);
}
}
FileOutputStream fo;
try {
fo = new FileOutputStream(f);
fo.write(bmpStream.toByteArray());
fo.flush();
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
Kotlin Way
if (file.length() > MAX_IMAGE_SIZE) {
var streamLength = MAX_IMAGE_SIZE
var compressQuality = 105
val bmpStream = ByteArrayOutputStream()
while (streamLength >= MAX_IMAGE_SIZE && compressQuality > 5) {
bmpStream.use {
it.flush()
it.reset()
}
compressQuality -= 5
val bitmap = BitmapFactory.decodeFile(file.absolutePath, BitmapFactory.Options())
bitmap.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream)
val bmpPicByteArray = bmpStream.toByteArray()
streamLength = bmpPicByteArray.size
if (BuildConfig.DEBUG) {
Log.d("test upload", "Quality: $compressQuality")
Log.d("test upload", "Size: $streamLength")
}
}
FileOutputStream(file).use {
it.write(bmpStream.toByteArray())
}
}
Constant
companion object {
//2000 * 1024 = 2 MB
private const val MAX_IMAGE_SIZE = 2048000
}