I've hit a wall with my Android-app, as I'm getting an OutOfMemoryException
with the description that my "bitmap exceeds VM budget" and so on. The weird thing is that the code works on one PC but triggers this exception in another.
I have like 10 900x500 images and the code I'm using is enclosed below. I have tried to recycle()
, which renders both the errors and the images invisible. I really need help with this. Thanks in advance:)
public class AVMSystemActivity extends Activity implements Runnable{
/** Called when the activity is first created. */
Field[] drawables;
ImageView imageView;
boolean adFlip=true;
Resources a;
int k=0x7f020000;
int last;
private RefreshHandler mRedrawHandler = new RefreshHandler();
int adCount=0;
RelativeLayout ll;
class RefreshHandler extends Handler {
@Override
public void handleMessage(Message msg) {
AVMSystemActivity.this.updateAd();
}
public void sleep(long delayMillis) {
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("TEST","A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
drawables = sepekin.software.R.drawable.class.getFields();
a=this.getResources();
int i=0x7f020000;
boolean done=true;
ll = (RelativeLayout) findViewById(R.id.rlout);
imageView = new ImageView(this);
ll.addView(imageView);
try
{
for (i=0x7f020000;i<0x7f020040;i++)
{
String name=a.getResourceName(i);
name=name.substring(name.indexOf(':')+1);
name=name.substring(9);
if(name.startsWith("ad"))
{
Log.i("res tara",name);
//imageView.setImageDrawable(a.getDrawable(i));//------
//imageView.setBackgroundDrawable(a.getDrawable(i));
adCount++;
}
else
{
last=i;
Log.i("LAST ELSE",i+"");
break;
}
}
}
catch(Exception e)
{
last=i;
Log.i("LAST",i+"");
done=false;
e.printStackTrace();
}
Button btn=(Button)findViewById(R.id.restbutton);
btn.setOnClickListener(new View.OnClickListener开发者_如何学Python() {
public void onClick(View view) {
Log.i("ACTION","BUTTON CLICKED");
adFlip=!adFlip;
try
{
Intent i = new Intent(AVMSystemActivity.this,RestListActivity.class);
startActivity(i);
}
catch(Exception e)
{
Log.i("cort",e.toString());
}
}
});
updateAd();
}
int currentInt=0;
private void updateAd(){
Log.i("update","lan");
try
{
if(adFlip)
{
mRedrawHandler.sleep(1000);
//*******
if(k==last)
k=0x7f020000;
else
{
if(a.getResourceName(k).contains("icon"))
{
Log.i("update",a.getResourceName(k));
}
else
{
Log.i("update",a.getResourceName(k)+"__--");
imageView = new ImageView(this);
imageView.setImageDrawable(a.getDrawable(k));
imageView.setPadding(20, 0, 0, 0);
ll.addView(imageView);
//imageView.getDrawingCache().recycle();
}
k++;
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
You should call recycle()
on the Bitmap after you've added the Bitmap to your ImageView. Works for me.
精彩评论