我试图在列表视图中加载一些320×199的图像并为其调整大小,为此我使用下面的方法,并且在使用我的应用程序并退出它之后,如果我再次重新打开它,则会崩溃。
12-30 06:03:46.249 6664 6664 E AndroidRuntime com.WasafatPizza FATAL EXCEPTION: main
12-30 06:03:46.249 6664 6664 E AndroidRuntime com.WasafatPizza Process: com.WasafatPizza, PID: 6664
12-30 06:03:46.249 6664 6664 E AndroidRuntime com.WasafatPizza java.lang.OutOfMemoryError
12-30 06:03:46.249 6664 6664 E AndroidRuntime com.WasafatPizza at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
12-30 06:03:46.249 6664 6664 E AndroidRuntime com.WasafatPizza at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
12-30 06:03:46.249 6664 6664 E AndroidRuntime com.WasafatPizza at java.lang.StringBuilder.append(StringBuilder.java:216)
12-30 06:03:46.249 6664 6664 E AndroidRuntime com.WasafatPizza at com.startapp.android.publish.ads.splash.d.b(StartAppSDK:69)
public void generatecontent(String X){
DBHelper mDBHlpr = new DBHelper(this);
Cursor res = mDBHlpr.getAllData(X);
int id;
Drawable d;
Bitmap newBitmap;
recipe = new String[res.getCount()];
ingredients = new String[res.getCount()];
steps = new String[res.getCount()];
images = new int[res.getCount()];
bitmaps = new BitmapDrawable[res.getCount()];
res.moveToFirst();
for (int j =0;j<res.getCount();j++){
recipe[j] = res.getString(2);
ingredients[j] = res.getString(3);
steps[j] = res.getString(4);
id = getResources().getIdentifier(res.getString(5), "drawable", getPackageName());
images[j] = id;
d = getResources().getDrawable(id);
newBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(),id),d.getIntrinsicWidth()/3,d.getIntrinsicHeight()/3,false);
bitmaps[j] = new BitmapDrawable(getResources(),newBitmap);
res.moveToNext();
}
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), recipe, bitmaps);
listview.setAdapter(customAdapter);
listview.setOnItemClickListener(this);
res.close();
}
答案 0 :(得分:0)
由于res.moveToNext()
位于for
中,因此导致您的应用程序内存不足。
在Android中循环遍历Cursor
的最简单方法是:
Cursor cursor = db.rawQuery(...);
try {
while (cursor.moveToNext()) { <--- Check if cursor has next item, then move to it
// do something here
}
} finally {
cursor.close(); <---- close the cursor
}