I have an activity that uses tabs. The content of the tab is pulled is pulled from the SQLite database. Everytime the user taps on a tab a call is made to the database and the content in the viewing area is displayed.
The content is list of ImageViews that, when clicked, interact with other areas of the activity. Since this can vary between 1 -> n number of items I'm not loading all the images at once and hiding views. Doing this could really cause some issue with loading the activity.
The tabs work but as I go through switching between tabs I end up getting an out of memory error:
E/AndroidRuntime(10469): FATAL EXCEPTION: main
E/AndroidRuntime(10469): java.lang.OutOfMemoryError
E/AndroidRuntime(10469): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
E/AndroidRuntime(10469): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:613)
E/AndroidRuntime(10469): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:439)
E/AndroidRuntime(10469): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:817)
E/AndroidRuntime(10469): at android.graphics.drawable.Drawable.createFromStream(Drawable.java:776)
E/AndroidRuntime(10469): at com.testapplication.testapplicationconfigurator.Configurator$2.createTabContent(Configurator.java:405)
E/AndroidRuntime(10469): at android.widget.TabHost$FactoryContentStrategy.getContentView(TabHost.java:735)
E/AndroidRuntime(10469): at android.widget.TabHost.setCurrentTab(TabHost.java:430)
E/AndroidRuntime(10469): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:161)
E/AndroidRuntime(10469): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:563)
E/AndroidRuntime(10469): at android.view.View.performClick(View.java:4383)
E/AndroidRuntime(10469): at android.view.View$PerformClick.run(View.java:18097)
E/AndroidRuntime(10469): at android.os.Handler.handleCallback(Handler.java:725)
E/AndroidRuntime(10469): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(10469): at android.os.Looper.loop(Looper.java:176)
E/AndroidRuntime(10469): at android.app.ActivityThread.main(ActivityThread.java:5279)
E/AndroidRuntime(10469): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(10469): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(10469): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
E/AndroidRuntime(10469): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
E/AndroidRuntime(10469): at dalvik.system.NativeStart.main(Native Method)
The image being used is a stored location on the devices such as
/storage/emulated/0/TestApplicaiton/2/maUdwcbaLU.jpg
I use the location to make it into a drawable object and place the drawable as the background to an ImageView:
ImageView iv = new ImageView(Configurator.this);
int ivHT = (int) (getResources().getDimension(
R.dimen.iv_height) / getResources()
.getDisplayMetrics().density);
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT);
// set margins (left,top,right,bottom)
params.setMargins(0, 0, 40, 0);
iv.setLayoutParams(params);
iv.getLayoutParams().height = ivHT;
iv.getLayoutParams().width = ivHT;
final String imageLoc = partData.get("productIndexImageLoc");
Log.d("imageLoc",imageLoc);
InputStream imageStream;
try {
imageStream = new FileInputStream(imageLoc);
Drawable x = Drawable.createFromStream(imageStream, null);
iv.setImageDrawable(x);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
partData is just a LinkedHashMap to store content and values for each part such as price, name, etc.
It seems pretty obvious that it has something to do with the Bitmap/Drawable I'm creating being left in memory. Is there a way to clear that used memory when someone taps a new tab or clear the item from memory once it's used in the view?