简体   繁体   中英

Android caused by java.lang.outofmemoryerror

I have this XML and code I added the codes from http://developer.android.com/training/displaying-bitmaps/load-bitmap.html as the guide says on how load bitmaps Efficiently but I'm still getting out of memory error from logcat. Any help

UPDATE I'M USING PICASSO NOW BUT STILL GETTING THE OUT OF MEMORY ERROR. THIS IS NUTS

I have this code ALREADY

ImageView targetImage;
ImageView targetImage2;
ImageView targetImage3;
ImageView targetImage4;
ImageView targetImage5;
ImageView targetImage6;
ImageView targetImage7;
ImageView targetImage8;
ImageView targetImage9;
ImageView targetImage10;
ImageView targetImage11;
ImageView targetImage12;
ImageView targetImage13;
ImageView targetImage14;

private Context context;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app3);
vf = (ViewFlipper) findViewById(R.id.fliping); 
targetImage = (ImageView) findViewById(R.id.imageView1);
targetImage2 = (ImageView) findViewById(R.id.imageView2);
targetImage3 = (ImageView) findViewById(R.id.imageView3);
targetImage4 = (ImageView) findViewById(R.id.imageView4);
targetImage5 = (ImageView) findViewById(R.id.imageView5);
targetImage6 = (ImageView) findViewById(R.id.imageView6);
targetImage7 = (ImageView) findViewById(R.id.imageView7);
targetImage8 = (ImageView) findViewById(R.id.imageView8);
targetImage9 = (ImageView) findViewById(R.id.imageView9);
targetImage10 = (ImageView) findViewById(R.id.imageView10);
targetImage11 = (ImageView) findViewById(R.id.imageView11);
targetImage12 = (ImageView) findViewById(R.id.imageView12);
targetImage13 = (ImageView) findViewById(R.id.imageView13);
targetImage14 = (ImageView) findViewById(R.id.imageView14);

Picasso.with(context).load(R.drawable.gr).resize(50, 50).centerCrop().into(targetImage); 
Picasso.with(context).load(R.drawable.calvitcdetails).resize(50, 50).centerCrop().into(targetImage2); 
Picasso.with(context).load(R.drawable.kapedetails).resize(50, 50).centerCrop().into(targetImage3); 
Picasso.with(context).load(R.drawable.glutafitdetails).resize(50, 50).centerCrop().into(targetImage4); 
Picasso.with(context).load(R.drawable.pgtdetails).resize(50, 50).centerCrop().into(targetImage5); 
Picasso.with(context).load(R.drawable.hilifedetails).resize(50, 50).centerCrop().into(targetImage6); 
Picasso.with(context).load(R.drawable.organicbarleyjuicedetails).resize(50, 50).centerCrop().into(targetImage7); 
Picasso.with(context).load(R.drawable.barleydetails).resize(50, 50).centerCrop().into(targetImage8); 
Picasso.with(context).load(R.drawable.spirulinadetails).resize(50, 50).centerCrop().into(targetImage9); 
Picasso.with(context).load(R.drawable.omnisoapdetails).resize(50, 50).centerCrop().into(targetImage10); 
Picasso.with(context).load(R.drawable.omnipinkishdetails).resize(50, 50).centerCrop().into(targetImage11); 
Picasso.with(context).load(R.drawable.omnidaydetails).resize(50, 50).centerCrop().into(targetImage12); 
Picasso.with(context).load(R.drawable.omnitonerdetails).resize(50, 50).centerCrop().into(targetImage13); 
Picasso.with(context).load(R.drawable.shuyadetails).resize(50, 50).centerCrop().into(targetImage14);


}

Any help

Yes, you could try using Picasso to load your images as it will give you automatic caching of images and memory management, also. I have always found resize() method of Picasso helpful.

Also, consider using RelativeLayout since you are nesting way too many LinearLayout s for displaying a single ImageView and this is a performance-killer.

The tricky thing about OutOfMemoryError s is that they don't necessarily have anything to do with the line of code where the crash occurred. If your app has a memory leak, it could be anywhere. The allocation that used the last bit of memory might be completely unrelated to the leak. You need to learn how to use a memory analyzer like Eclipse MAT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM