Tôi cần gửi hình ảnh từ tệp đến máy chủ. Máy chủ yêu cầu hình ảnh ở độ phân giải 2400x2400.BitmapFactory.decodeFile hết bộ nhớ với hình ảnh 2400x2400
Những gì tôi đang cố gắng làm là:
1) Nhận một Bitmap sử dụng BitmapFactory.decodeFile sử dụng inSampleSize đúng.
2) Nén hình ảnh trong JPEG với chất lượng 40%
3) Mã hóa hình ảnh trong base64
4) Gửi đến các máy chủ
tôi không thể đạt được bước đầu tiên, nó ném ra một ngoại lệ bộ nhớ. Tôi chắc chắn inSampleSize là chính xác nhưng tôi cho rằng ngay cả với inSampleSize Bitmap là rất lớn (khoảng 30 MB trong DDMS).
Bất kỳ ý tưởng nào có thể thực hiện được? Tôi có thể thực hiện các bước này mà không cần tạo một đối tượng bitmap không? Tôi có nghĩa là làm nó trên hệ thống tập tin thay vì bộ nhớ RAM.
Đây là mã hiện tại:
// The following function calculate the correct inSampleSize
Bitmap image = Util.decodeSampledBitmapFromFile(imagePath, width,height);
// compressing the image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 40, baos);
// encode image
String encodedImage = Base64.encodeToString(baos.toByteArray(),Base64.DEFAULT));
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height/(float) reqHeight);
final int widthRatio = Math.round((float) width/(float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromFile(String path,int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path,options);
}
làm qua u sẽ nhận được một số ý tưởng http://developer.android.com/training/displaying-bitmaps/manage-memory.html –
getBitmap(). recycle(); –
Bạn có hình ảnh trong một tệp, phải không? Chỉ cần gửi tệp này đến máy chủ và thực hiện các phép biến đổi cần thiết tại đó. – Henry