我有一个

Android应用程序,需要一些照片,重新调整大小,并将它们发送到后端服务器.除三星Galaxy S3外,所有其他手机(姜饼和冰淇淋三明治)均可使用此应用程序.每当它拍照并尝试重新调整大小时,它的内存就会耗尽.最初我认为这是重新调整部分的一个问题,我实施了位图对象,并且尝试了所有内容,但仍然保持崩溃.然后我意识到,当应用程序启动时,它正在使用大量的内存.在HTC和所有其他手机上,当我的应用程序启动时,它使用了大约4 MB,但在Galaxy S3上,它占用了大约30MG,几乎是10倍.我似乎无法弄明白什么会导致它占用更多的内存.我正在回收所有的对象和布局(我想).这是我必须调整图像大小的代码:

public static void resizeImage(String pathOfInputImage,

String pathOfOutputImage) {

try {

int inWidth = 0;

int inHeight = 0;

float factor;

InputStream in = new FileInputStream(pathOfInputImage);

BitmapFactory.Options options = new BitmapFactory.Options();

options.inJustDecodeBounds = true;

BitmapFactory.decodeStream(in, null, options);

inWidth = options.outWidth;

inHeight = options.outHeight;

in.close();

in = null;

options = null;

if (inWidth > inHeight) {

factor = (float) inHeight / 480;

} else {

factor = (float) inWidth / 480;

}

options = new BitmapFactory.Options();

Bitmap roughBitmap;

if(Build.VERSION.SDK_INT < 12) {

System.gc();

}

try

{

in = new FileInputStream(pathOfInputImage);

options.inSampleSize = 4;

roughBitmap = BitmapFactory.decodeStream(in, null, options);

}

catch (OutOfMemoryError e)

{

roughBitmap = Utils.fetchRoughBitmap(pathOfInputImage, factor);

}

finally

{

roughBitmap = Utils.fetchRoughBitmap(pathOfInputImage, factor);

}

in.close();

in = null;

boolean rotate = false;

Bitmap rotatedBitmap = null;

if (isNeedToRotate(pathOfInputImage)) {

rotate = true;

Matrix m = new Matrix();

m.postRotate(90);

try

{

rotatedBitmap = Bitmap.createBitmap(roughBitmap, 0, 0,

roughBitmap.getWidth(), roughBitmap.getHeight(), m,

true);

}

catch (OutOfMemoryError e)

{

rotatedBitmap = Bitmap.createBitmap(roughBitmap, 0, 0,

roughBitmap.getWidth()/2, roughBitmap.getHeight()/2, m,

true);

}

}

if (rotate) {

Utils.log(TAG, "Rotate Invoked :------->");

int temp = inHeight;

inHeight = inWidth;

inWidth = temp;

}

options.inSampleSize = Math.round(factor);

float dstWidth = (int) inWidth / factor;

float dstHeight = (int) inHeight / factor;

Bitmap resizedBitmap;

if (rotate) {

try

{

resizedBitmap = Bitmap.createScaledBitmap(rotatedBitmap,

(int) dstWidth, (int) dstHeight, true);

}

catch (OutOfMemoryError e)

{

resizedBitmap = Bitmap.createScaledBitmap(rotatedBitmap,

(int) dstWidth/2, (int) dstHeight/2, true);

}

} else {

try

{

resizedBitmap = Bitmap.createScaledBitmap(roughBitmap,

(int) dstWidth, (int) dstHeight, true);

}

catch (OutOfMemoryError e)

{

resizedBitmap = Bitmap.createScaledBitmap(roughBitmap,

(int) dstWidth/2, (int) dstHeight/2, true);

}

}

try

{

FileOutputStream out = new FileOutputStream(pathOfOutputImage);

resizedBitmap.compress(Bitmap.CompressFormat.PNG, 80, out);

}

catch (Exception e) {

Utils.log("Image", e.getMessage() + e);

}

//Free up the memory.

roughBitmap.recycle();

resizedBitmap.recycle();

if (rotatedBitmap != null) {

rotatedBitmap.recycle();

}

} catch (IOException e) {

Utils.log("Image", e.getMessage() + e);

}

}

private static Bitmap fetchRoughBitmap(String pathOfInputImage, float factor)

{

Bitmap roughBitmap = null;

try

{

FileInputStream in = new FileInputStream(pathOfInputImage);

BitmapFactory.Options options = new BitmapFactory.Options();

options.inSampleSize = Math.round(factor);

roughBitmap = BitmapFactory.decodeStream(in, null, options);

return roughBitmap;

}

catch (OutOfMemoryError mem_error)

{

return Utils.fetchRoughBitmap(pathOfInputImage, factor+1);

}

catch (FileNotFoundException e)

{

//return Utils.fetchRoughBitmap(pathOfInputImage, factor+1);

return roughBitmap;

}

}

此代码工作正常,因为我通过递归循环运行它,直到它没有用尽内存,但是我生成的图像只有大约300×300像素,我需要它至少为640×480.我的问题是:

>任何人都可以告诉我,为什么Galaxy S3在HTC手机上首次加载我的应用而不是60%时,会占用高达90%的内存.

>在使我的应用程序运行内存不足的重新调整代码中有什么问题.

任何帮助将不胜感激,因为我已经花了3天这个问题:-(.

更多推荐