作者:sdfsfsd | 来源:互联网 | 2023-05-20 10:35
1> stephen1706..:
好的,我得到了自己的答案
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();
}