mylottofinder.com.au mylottofinder.com.au - 1 month ago 9
Android Question

"OutOfMemoryError" When scrolling images in the ListView

10-16 21:49:03.318 29852-29852/com.klau.jobsinaustralia I/dalvikvm-heap: Grow heap (frag case) to 171.874MB for 14400016-byte allocation
10-16 21:49:03.578 29852-29852/com.klau.jobsinaustralia I/dalvikvm-heap: Grow heap (frag case) to 175.314MB for 14400016-byte allocation
10-16 21:49:03.689 29852-29852/com.klau.jobsinaustralia I/dalvikvm-heap: Grow heap (frag case) to 177.603MB for 3600016-byte allocation
10-16 21:49:03.839 29852-29852/com.klau.jobsinaustralia I/dalvikvm-heap: Grow heap (frag case) to 178.716MB for 14364016-byte allocation
10-16 21:49:04.029 29852-29852/com.klau.jobsinaustralia I/dalvikvm-heap: Grow heap (frag case) to 182.185MB for 14400016-byte allocation
10-16 21:49:04.139 29852-29852/com.klau.jobsinaustralia I/dalvikvm-heap: Forcing collection of SoftReferences for 3600016-byte allocation
10-16 21:49:04.199 29852-29852/com.klau.jobsinaustralia E/dalvikvm-heap: Out of memory on a 3600016-byte allocation.
10-16 21:49:04.199 29852-29852/com.klau.jobsinaustralia I/dalvikvm: "main" prio=5 tid=1 RUNNABLE
10-16 21:49:08.223 29852-29852/com.klau.jobsinaustralia E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.OutOfMemoryError: (Heap Size=194912KB, Allocated=182627KB)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:628)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:475)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:504)
at com.klau.jobsinaustralia.scrollable.CustomList.decodeSampledBitmapFromResource


Activity

public class ExtraInfoActivity extends BaseActivity {

ListView list;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
final String languageToLoad = sharedPreferences.getString("PREF_LIST", "no selection");
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());

setContentView(R.layout.activity_extra_info);
String[] nsw_attraction = {
"Sydney Bridge",
"Blue Mountains",
"Bondi Beach",
"The Hunter Valley",
"The Jenolan Caves"
};


final String[] nsw_web = {
getResources().getString(R.string.nsw_attraction_1),
getResources().getString(R.string.nsw_attraction_2),
getResources().getString(R.string.nsw_attraction_3),
getResources().getString(R.string.nsw_attraction_4),
getResources().getString(R.string.nsw_attraction_5)
};

Integer[] nswImageId = {
R.drawable.nsw_attraction_bridge,
R.drawable.nsw_attraction_blue_mountains,
R.drawable.nsw_attraction_bondi_beach,
R.drawable.nsw_attraction_hunter_valley,
R.drawable.nsw_attraction_jenolan_caves
};
CustomList adapter = new CustomList(ExtraInfoActivity.this, nsw_attraction, nsw_web, nswImageId);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ExtraInfoActivity.this, nsw_web[+position], Toast.LENGTH_SHORT).show();
}
});
}

@Override
public void onStart() {
super.onStart();
try {
mTracker.setScreenName("Extra Infomation");
mTracker.send(new HitBuilders.AppViewBuilder().build());
}catch (Exception ex){
ex.printStackTrace();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
}


}

Answer

You need to use a library to load the images in the ListView.

Glide is an amazing library to load the images.

its setup is very simple:

put these two in your build.gradle

dependencies {
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.android.support:support-v4:19.1.0'
}

and then in your adapter all you need to do is:

Glide
    .with(context)
    .load(nswImageId[position])
    .into(myImageView);

Thats it, it will take care of everything. You can find more informatin and usage details here.

Comments