在我需要上傳圖像的服務器上有2MB限制。上傳前下載圖像大小限制下限
我使用此方法來下采樣位圖Strange out of memory issue while loading an image to a Bitmap object
此方法
public InputStream getPhotoStream(int imageSizeBytes) throws IOException {
int targetLength = 1500;
ByteArrayOutputStream photoStream;
byte[] photo;
Bitmap pic;
final int MAX_QUALITY = 100;
int actualSize = -1;
do {
photo = null;
pic = null;
photoStream = null;
//this calls the downsampling method
pic = getPhoto(targetLength);
photoStream = new ByteArrayOutputStream();
pic.compress(CompressFormat.JPEG, MAX_QUALITY, photoStream);
photo = photoStream.toByteArray();
actualSize = photo.length;
targetLength /= 2;
} while (actualSize > imageSizeBytes);
return new ByteArrayInputStream(photo);
}
這將引發的OutOfMemoryError在第二次迭代內。我怎樣才能將圖像縮小到一定的大小限制以下?
試試這個。 http://blog.androidquery.com/2011/05/down-sample-images-to-avoid-out-of.html –