在我的应用程序中,我正在从网络上下载图像.为此,我第一次从Web下载图像,这些图像存储在sdcard中.下次,我正在检查这些图像是否在sdcard中.如果是的话,从sdcard提取,否则我从网上下载.这些图像显示为列表.我反复(意味着连续向上/向下移动)滚动列表,然后我的应用程序崩溃了,我遇到了内存不足的异常.如何处理.有谁能够帮助我.

堆栈跟踪:

09-12 13:40:42.640: ERROR/AndroidRuntime(3426): java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=7879KB, Allocated=3362KB, Bitmap Size=11402KB)

09-12 13:40:42.640: ERROR/AndroidRuntime(3426): at android.graphics.BitmapFactory.nativeDecodeFile(Native Method)

09-12 13:40:42.640: ERROR/AndroidRuntime(3426): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:355)

09-12 13:40:42.640: ERROR/AndroidRuntime(3426): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:433)

09-12 13:40:42.640: ERROR/AndroidRuntime(3426): at com.ibkr.elgifto.GiftCategories$itemlistadapter$4.getDrawableFromUrl(GiftCategories.java:830)

09-12 13:40:42.640: ERROR/AndroidRuntime(3426): at com.ibkr.elgifto.GiftCategories$itemlistadapter$4.run(GiftCategories.java:739)

09-12 13:53:32.450: INFO/WSP(332): [Receiver] next auto-sync alarm event is 180 mins later, at time: Mon Sep 12 16:53:32 GMT+05:30 2011

private Drawable getDrawableFromUrl(String imageUrl) {

// TODO Auto-generated method stub

String filename = imageUrl;

filename = filename.replace("/", "+");

filename = filename.replace(":", "+");

filename = filename.replace("~", "s");

File elgiftoimagesref = new File("/sdcard/Elgiftoimages/");

elgiftoimagesref.mkdir();

final File file = new File(elgiftoimagesref, filename);

BitmapDrawable SDdrawable = null;

boolean exists = file.exists();

if (!exists) {

try {

URL myFileUrl = new URL(imageUrl);

HttpURLConnection conn = (HttpURLConnection) myFileUrl

.openConnection();

conn.setDoInput(true);

conn.connect();

InputStream is = conn.getInputStream();

final Bitmap result = BitmapFactory.decodeStream(is);

is.close();

new Thread() {

public void run() {

ByteArrayOutputStream bytes = new ByteArrayOutputStream();

if (result != null) {

result.compress(Bitmap.CompressFormat.JPEG, 40,

bytes);

}

try {

FileOutputStream fo;

fo = new FileOutputStream(file);

fo.write(bytes.toByteArray());

fo.flush();

fo.close();

// result.recycle();

} catch (IOException e) {

e.printStackTrace();

}

}

}.start();

BitmapDrawable returnResult = new BitmapDrawable(result);

return returnResult;

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return null;

} else {

// Here i am getting the out of memory error.

SDdrawable = new BitmapDrawable(BitmapFactory.decodeFile(file

.toString()));

return SDdrawable;

}

}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐