-1

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?

dcp3450
  • 10,959
  • 23
  • 58
  • 110
  • Check [this](http://stackoverflow.com/questions/21114632/android-out-of-memory-error-when-loading-images-from-resource) out. – Alejandro Cumpa Jan 23 '14 at 16:17
  • The link in the suggested dup to the android docs ended up helping. The image (which I haver very little control over) is very big and I was resizing it incorrectly. Using [Loading Large Bitmaps Efficiently](http://developer.android.com/training/displaying-bitmaps/load-bitmap.html) helped. Now I can switch back and forth with no error. Sorry for the dup I was just using the wrong keywords while searching. – dcp3450 Jan 23 '14 at 16:48

1 Answers1

0

Yes when the bitmap is not more usefull you can call:

bitmap.recycle()

If you not have this possibility you can add more heap in manifest(Onli > API 2.3):

android:largeHeap="true"

I suggest to read this documentation to find the good way to work with bitmap in android:

Android bitmap memory management

phemt.latd
  • 1,775
  • 21
  • 33