title: Android: Memory leak causing OOM when using setImageURI
body: <p>i've only programmed java a couple of days so be gentle... :P </p>
  
  <p>My app uses a SQLite database to save different "projects" that needs user provided text, photo and location. The problem is that when browsing through some of the project a Out of Memory-exception occurs when loading the </p>
  
  <blockquote>
    <p>myImage.setImageURI(Uri.parse(msavePicture));</p>
  </blockquote>
  
  <p>for the fourth of fifth time. When using DDMS you can see that the allocated memory increases about 0.2Mbyte every new load. I've searched the problem and tried using </p>
  
  <blockquote>
    <p>myImage.setImageDrawable(null);</p>
  </blockquote>
  
  <p>but the issue remains. I've also tried changing the code to </p>
  
  <blockquote>
  <pre><code>  myBitmap = BitmapFactory.decodeFile(msavePicture);
    ImageView myImage = (ImageView) this.findViewById(R.id.camera_preview);
    myImage.setImageBitmap(myBitmap);   
  </code></pre>
  </blockquote>
  
  <p>so i could use .recycle(); but the picture doesent load, it returns null.
  msavePicture is a string that looks like this:</p>
  
  <blockquote>
    <p>file:///mnt/sdcard/MyCameraApp/IMG_2011-12-09-01-49.jpg</p>
  </blockquote>
  
  <p>i've included </p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;
  </code></pre>
  
  <p>in manifest and i know the picture exists because when using setImageURI it works.</p>
  
  <p>So the question is: What am i doing wrong to cause the memory leak, how to truly destroy the ImageView, or why can't Bitmapfactory decode my string, is the file location wrong?</p>
  
  <p>I've included alot of the activity just in case.  Any solution or links regarding this would be greatly appreciated!</p>
  
  <p>Thanks in advance!</p>
  
  <p>(PS: If someone knows a guide how to save a SQLite database to a server so you can update other phones with it would also be greatly appreciated! =)   :DS)  </p>
  
  <pre><code>  public class Page3 extends Activity {
  private static final int ACTIVITY_CREATE = 0;
  public static Long Row = (long) 40;
  public static String summarydec = "FAIL";
  public static String msavePicture;
  public static Location loc;
  private EditText mgps_lati;
  private EditText mgps_long;
  static String gps_long;
  static String gps_lati;
  private TodoDbAdapter mDbHelper;
  public Location myLocation;
  public static Bitmap myBitmap;
  public Uri uBitmap;
  
  
  @Override
  protected void onCreate(Bundle bundle) {
      super.onCreate(bundle);
      mDbHelper = new TodoDbAdapter(this);
      mDbHelper.open();
      setContentView(R.layout.camera);
  
  
          if(msavePicture == null){
          msavePicture = "";
      }
   //     myBitmap = BitmapFactory.decodeFile(msavePicture);
   //     ImageView myImage = (ImageView) this.findViewById(R.id.camera_preview);
   //     myImage.setImageBitmap(myBitmap);       
  
      ImageView myImage = (ImageView) this.findViewById(R.id.camera_preview);
      myImage.setImageURI(Uri.parse(msavePicture));
  
      mgps_long = (EditText) findViewById(R.id.gps_long); 
      mgps_lati = (EditText) findViewById(R.id.gps_lati);
      mgps_lati.setText("Latitude:  "+gps_lati);
      mgps_long.setText("Longitude: "+gps_long);
      ////////////////////////////////////////////////////////////
      ///// G P S  -  L Y S S N A R E ////////////////////////////
      ////////////////////////////////////////////////////////////    
      MyLocation myLocation = new MyLocation();
      myLocation.getLocation(this, locationResult);
  
      Button gps = (Button) findViewById(R.id.gps);
      gps.setOnClickListener(new View.OnClickListener() {
          public void onClick(View view) {
  
              if (gps_lati != null &amp;&amp; gps_long != null) {
                  mgps_lati.setText("Latitude:  " +gps_lati);
                  mgps_long.setText("Longitude: " +gps_long);
                  TodoDetails.save_gps_long = gps_long;
                  TodoDetails.save_gps_lati = gps_lati;
                  Page1.save_gps_long = gps_long;
                  Page1.save_gps_lati= gps_lati;
                  Page2.save_gps_long = gps_long;
                  Page2.save_gps_lati= gps_lati;
                  } else {
                      String Latitude = "Fel vid GPS-hanteringen, arra det!";
                      String Longitude = "Fel vid GPS-hanteringen, arra det!";
                      mgps_lati.setText(Latitude);
                      mgps_long.setText(Longitude);
                  }
          }});
      ////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////
  
      Button camera = (Button) findViewById(R.id.camera);
      camera.setOnClickListener(new View.OnClickListener() {
          public void onClick(View view) {                
              createCamera();
          }});
  
      Button confirmButton = (Button) findViewById(R.id.todo_edit_button);
      confirmButton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View view) {                
              setResult(RESULT_OK);
              TodoDetails.mPIC = msavePicture;
              Page1.mPIC = msavePicture;
              Page2.mPIC = msavePicture;
              ImageView myImage = (ImageView) findViewById(R.id.camera_preview);
              myImage.setImageDrawable(null);
   //             myBitmap.recycle();
              cancelTimer();
              finish();
          }
  
      });
  }
  protected void onPause() {
      super.onPause();
      cancelTimer();
      ImageView myImage = (ImageView) findViewById(R.id.camera_preview);
      myImage.setImageDrawable(null);
  
  }
  protected void cancelTimer()
  {
      MyLocation myLocation = new MyLocation();
      myLocation.cancelTimer();   
  }
  protected void onActivityResult(int requestCode, int resultCode,
          Intent intent) {
      super.onActivityResult(requestCode, resultCode, intent);
      cancelTimer();  
      ImageView myImage = (ImageView) findViewById(R.id.camera_preview);
      myImage.setImageURI(Uri.parse(msavePicture));
  
   //     ImageView myImage = (ImageView) this.findViewById(R.id.camera_preview);
   //     myBitmap = BitmapFactory.decodeFile(msavePicture);
   //     myImage.setImageBitmap(myBitmap);
  
  }
  public void onBackPressed (){
      cancelTimer();  
      ImageView myImage = (ImageView) findViewById(R.id.camera_preview);
      myImage.setImageDrawable(null);
   //     Bitmap myBitmap = Page3.myBitmap;
  //      myBitmap.recycle();
      finish();
  }
  ////////////////////////////////////////////////////
  ////////////// A L L A   V Y E R ///////////////////
  ////////////////////////////////////////////////////
  private void createPage1() {
      Intent b = new Intent(this, Page1.class);
      startActivityForResult(b, ACTIVITY_CREATE);
      setResult(RESULT_OK, b);
      finish();
  }
  private void createPage2() {
      Intent b = new Intent(this, Page2.class);
      startActivityForResult(b, ACTIVITY_CREATE);
      setResult(RESULT_OK, b);
  }
  private void createTodo() {
      Intent i = new Intent(this, TodoDetails.class);
      startActivityForResult(i, ACTIVITY_CREATE);
      setResult(RESULT_OK, i);
  }
  private void createCamera() {
      Intent c = new Intent(this, Camera.class);
      startActivityForResult(c, ACTIVITY_CREATE);
      setResult(RESULT_OK, c);
  }
  //////////////////////////////////////////////////////////
  //////// M E N U  T O O L B A R //////////////////////////
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.projektlist, menu);
      return true;
  }
  public LocationResult locationResult = new LocationResult(){
      @Override
      public void gotLocation(final Location location){
          gps_lati = Double.toString(location.getLatitude());
          gps_long = Double.toString(location.getLongitude());
          }};
   }
  </code></pre>