我需要將圖像從文件發送到服務器。服務器請求圖像的分辨率爲2400x2400。BitmapFactory.decodeFile內存不足與圖像2400x2400
我試圖做的是:
1)使用BitmapFactory.decodeFile使用正確的inSampleSize得到一個位圖。
2)壓縮的JPEG圖像具有40%
3)質量編碼所述圖像中向服務器發送
的base64
4)我不能達到的第一步,它引發內存不足異常。我確定inSampleSize是正確的,但是即使在inSampleSize中,位圖也很大(在DDMS中大約爲30 MB)。
任何想法如何做到這一點?我可以在不創建位圖對象的情況下執行這些步驟嗎?我的意思是在文件系統而不是RAM內存上做。
這是當前的代碼:
// 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);
}
至u做會得到一些想法http://developer.android.com/training/displaying-bitmaps/manage-memory.html –
getBitmap()。recycle(); –
你有一個文件中的圖像,對吧?只需將此文件發送到服務器並在那裏進行必要的轉換。 – Henry