I am having a memory leak and the solutions I've found when searching the web haven't been much help. I know what the problem is, I just don't know how to fix it. The problem is that I'm allocating images as final (actually stored in a final linearlayout view) and they don't get deallocated. But I'm not sure how to do that because of the way I'm creating them.
The problem happens on a screen rotation, when the activity is destroyed and recreated.
Any suggestions are welcome and thanks for your time. My code and error log are below.
The problem happens on a screen rotation, when the activity is destroyed and recreated.
Any suggestions are welcome and thanks for your time. My code and error log are below.
Code:
db.open();
Cursor cursor1 = db.getItemFromInfo("picvid", iid);
if(cursor1.moveToFirst())
{
while(!cursor1.isAfterLast())
{
String uriString = cursor1.getString(2);
final Uri uri2 = Uri.parse(uriString);
String[] projection = { MediaStore.Images.ImageColumns.DATA, /*col1*/
MediaStore.Images.ImageColumns.DISPLAY_NAME /*col2*/};
Cursor c = managedQuery(uri2, projection, null, null, null);
if (c!=null && c.moveToFirst()) {
name = c.getString(1);
Log.d("Display name",name);
}
c.close();
if(!name.equals(""))
{
if(name.toLowerCase().contains(".jpg"))
{
final LinearLayout ll3 = new LinearLayout(this);
ll3.setOrientation(LinearLayout.HORIZONTAL);
ll3.setLayoutParams(params);
ll.addView(ll3);
LinearLayout ll4 = new LinearLayout(this);
ll4.setOrientation(LinearLayout.VERTICAL);
ll4.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));
final int id = cursor1.getInt(0);
ImageView iv = new ImageView(this);
iv.setImageURI(uri2);
iv.setAdjustViewBounds(true);
iv.setMaxHeight(100);
iv.setMaxWidth(100);
iv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
iv.setClickable(true);
iv.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent i = new Intent(PicVid.this, Picture.class);
//create bundle and initialize
Bundle bundle = new Bundle();
//add parameters to bundle
bundle.putString("uri", uri2.toString());
//add bundle to the intent
i.putExtras(bundle);
startActivity(i);
}});
//((LinearLayout.LayoutParams) iv.getLayoutParams()).gravity = Gravity.CENTER;
ll3.addView(iv);
ll3.addView(ll4);
TextView captionTitle = new TextView(this);
captionTitle.setText("Caption:");
ll4.addView(captionTitle);
TextView caption = new TextView(this);
caption.setText(cursor1.getString(1));
ll4.addView(caption);
final View ruler3 = new View(this);
ruler3.setBackgroundColor(0xFF909090);
ruler3.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 2));
ImageButton delImage = new ImageButton(this);
delImage.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
delImage.setImageResource(R.drawable.delete);
delImage.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
db.open();
db.deleteItem("picvid", id, iid);
db.close();
ll.removeView(ll3);
ll.removeView(ruler3);
}
});
((LinearLayout.LayoutParams) delImage.getLayoutParams()).gravity = Gravity.CENTER;
ll3.addView(delImage);
ll.addView(ruler3);
}
}
cursor1.moveToNext();
}
}
cursor1.close();
db.close();
Code:
06-10 09:23:34.612: ERROR/AndroidRuntime(800): Uncaught handler: thread main exiting due to uncaught exception
06-10 09:23:34.622: ERROR/AndroidRuntime(800): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:375)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:212)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:663)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.graphics.drawable.Drawable.createFromStream(Drawable.java:641)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.widget.ImageView.resolveUri(ImageView.java:491)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.widget.ImageView.setImageURI(ImageView.java:287)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at mobile.PicVid.onCreate(PicVid.java:202)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at mobile.PicVid.onResume(PicVid.java:701)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1228)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.app.Activity.performResume(Activity.java:3542)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2619)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:170)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.app.LocalActivityManager.dispatchResume(LocalActivityManager.java:518)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.app.ActivityGroup.onResume(ActivityGroup.java:58)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1228)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.app.Activity.performResume(Activity.java:3542)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2619)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2647)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2287)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3278)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.app.ActivityThread.access$1900(ActivityThread.java:112)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.os.Handler.dispatchMessage(Handler.java:99)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.os.Looper.loop(Looper.java:123)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at android.app.ActivityThread.main(ActivityThread.java:3948)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at java.lang.reflect.Method.invokeNative(Native Method)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at java.lang.reflect.Method.invoke(Method.java:521)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
06-10 09:23:34.622: ERROR/AndroidRuntime(800): at dalvik.system.NativeStart.main(Native Method)