title: Unable to increment counter using SharedPreferences in Android
body: <p>I am just a fresher in Android and I have a scenario where the user can take a photo using camera and the photo would be stored in SDCard. I need to save the photos with a dynamic name i.e, I want the image name as Image 1,Image 2 and so on. I set the counter value as 1 initially so the first image would be stored as Image 1 and next time the counter would increase the value and store again. </p>
  
  <p><strong>Issue :</strong></p>
  
  <p>My problem here is that I am not able to increase the counter value every time I close and re-open the app. I have tried a lot and it sets the counter back to 1 again.</p>
  
  <p><strong>CODE :</strong></p>
  
  <pre><code>int count;
  SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
          int defaultValue = getPreferences(MODE_PRIVATE).getInt("count_key",count);
          defaultValue = count;
          editor.putInt("count_value",defaultValue).commit();
          if(count == 1)
          {
              count = 1;
              getPreferences(MODE_PRIVATE).edit().putInt("count_key",count).commit();
  
          }
          else
          {
               ++count;
              getPreferences(MODE_PRIVATE).edit().putInt("count_key",count).commit();
              count = getPreferences(MODE_PRIVATE).getInt("count_key",count);
              System.out.println("The count value is " + count);
          }
  </code></pre>
  
  <p>On executing, it returns the count value as 1 every time and does not seem to increment the count value. It should be 1 the first time i open it and then 2 and so on. Is there a problem in my logic or am going wrong with SharedPrefs. I have surfed a lot on StackOverFlow but did not seem to come across with the style that I need. Any help would be highly appreciated guys. Thanks in advance.</p>