我是android的新手,任何人都可以告诉我如何编写有关内存管理的有效代码。
public class XXX extends Activity {
EditText et;
ImageView iv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) findViewById(R.id.editText);
iv = (ImageView) findViewById(R.id.iv);
}
public void release(){
et=null;
Drawable drawable = ((ImageView) iv).getDrawable();
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
bitmapDrawable.getBitmap().recycle();
}
iv=null;
System.gc();
}
public void onPic(View v){
// code to capture image
}
public void onSave(View v){
DBHelper dbHelper = new DBHelper(this);
SqliteDatabase db = dbHelper.getReadableDatabase();
String name = et.getText().toString();
ContentValues cv cv= new ContentValues();
// saving , calling release(); and going back to main screen
}
我有什么办法可以避免内存不足,挂电话吗?
答案 0 :(得分:0)
Bitmap不会轻易释放内存,因为这会导致内存不足异常。您可以调整位图的大小并加载它。
这可能会帮助您显示有效的位图加载: Bitmap loading
您可以从这里查看完整的理解文档 Bitmap handling