title: How to save an image path in SharedPreferences in Android app? my code dont work
body: <p>In my app the user selects a pic from clicking a button directing them to their gallery. They chose the pic but once I exit the app the pic has gone. How can I save it? How can I save it with SharedPeferences?</p>
  
  <pre><code>import android.support.v7.app.ActionBarActivity;
  import android.content.Intent;
  import android.database.Cursor;
  import android.graphics.BitmapFactory;
  import android.net.Uri;
  import android.os.Bundle;
  import android.provider.MediaStore;
  import android.view.View;
  import android.widget.Button;
  import android.widget.ImageView;    
  import android.content.SharedPreferences;
  import android.content.SharedPreferences.Editor;
  import android.preference.PreferenceManager;
  
  
  
  
  public class MyNewScreen extends ActionBarActivity {
  
  
  
   @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.my_new_screen);
  
  
  
          SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
              String picturePath = prefs.getString("profilePic", "");
              if(!picturePath.equals("")){
                    ImageView imageView = (ImageView) findViewById(R.id.imageView1);
                    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
  
  
  
              }
  
  
  Button buttonLoadImage = (Button) findViewById(R.id.button1);
  
  
          buttonLoadImage.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
  
                  Intent i = new Intent(
                          Intent.ACTION_PICK,
                          android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  
                  startActivityForResult(i, RESULT_LOAD_IMAGE);
  
  
              }
          });
  
   }
  
  
          private static int RESULT_LOAD_IMAGE = 1;
  
          @Override
          protected void onActivityResult(int requestCode, int resultCode, Intent data) {
              super.onActivityResult(requestCode, resultCode, data);
  
              if (requestCode == RESULT_LOAD_IMAGE &amp;&amp; resultCode == RESULT_OK &amp;&amp; null != data) {
                  Uri selectedImage = data.getData();
                  String[] filePathColumn = { MediaStore.Images.Media.DATA };
  
                  Cursor cursor = getContentResolver().query(selectedImage,
                          filePathColumn, null, null, null);
                  cursor.moveToFirst();
  
                  int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                  String picturePath = cursor.getString(columnIndex);
                  cursor.close();
  
                  ImageView imageView = (ImageView) findViewById(R.id.imageView1);
                  imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
  
  
  
                  SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
                  Editor edit=shre.edit();
                  edit.putString("profilePic", picturePath);
                  edit.commit();
  
                      }
                  }
  
  
          }
  </code></pre>