0

Hello I've made it so that in my app the user selects a profile pic from clicking a button directing them to their gallery. They chose the pic and it's fine but once I exit the app or delete from multi tasking the profile pic has gone. How can I save it? I know I need shared prefs but im not fully sure how to use it in code. I'm a beginner.

nitind
  • 19,089
  • 4
  • 34
  • 43
user3195144
  • 23
  • 1
  • 6
  • Save a reference to the profile pic, such as a file path, or url. See [this question](http://stackoverflow.com/questions/5734721/android-shared-preferences) for an example of sharedpreferences. – nhaarman Feb 12 '14 at 19:54

1 Answers1

0

you need to override the onPause() method and save your pics there. when you want to get them you call the getString method on the Shered prefs Object the second paramter is a default value if the the method cannot find the key. here it is:

package com.example.sharedpreferences;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.widget.EditText;

public class MainActivity extends Activity {
    EditText et1;
    EditText et2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et1=(EditText)findViewById(R.id.editText1);
        et2=(EditText)findViewById(R.id.editText2);
        SharedPreferences sp = getPreferences(Activity.MODE_PRIVATE);
        et1.setText(sp.getString("il.co.gal.text1", ""));
        et2.setText(sp.getString("il.co.gal.text2", ""));

    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
protected void onPause() {
    super.onPause();
    SharedPreferences sp = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor=sp.edit();
    editor.putString("il.co.gal.text1", et1.getText().toString());
    editor.putString("il.co.gal.text2", et2.getText().toString());
    editor.commit();

}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onRestoreInstanceState(savedInstanceState);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
}



}
Gal Rom
  • 6,221
  • 3
  • 41
  • 33