0

In my app I want to make a profile page with default image profile and allow to user to change it by take a picture from camera or choose image from gallery, I did that successfully and here's my code:

   public class MainActivity extends AppCompatActivity {
    private static final int pick = 1, capture = 2;
    Uri imgeUri, touri;
    ImageView imp;
    SharedPreferences sh;
    SharedPreferences.Editor editor;
    String S;
    boolean d=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("eee","in resume");

        setContentView(R.layout.activity_main);
        sh = getSharedPreferences("my" ,Context.MODE_PRIVATE);
        editor=sh.edit();
        imp = (ImageView) findViewById(R.id.profile_image);

        if(d==false) {
            imp.setImageResource(R.drawable.photo);
        }
        else{
            imp.setImageURI(Uri.parse(sh.getString("link", null)));
        }


}


public void changepic(View V) {
    final String[] items = {"Take picture", "Choose Picture", 
     "cancle"};

    AlertDialog.Builder build = new AlertDialog.Builder(this);
    build.setTitle("Add Photo");
    build.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (items[which].equals("Choose Picture")) {
                Log.d("test","bh");

                Intent i = new Intent(Intent.ACTION_PICK);
                i.setType("image/*");
                startActivityForResult(i, pick);

            } else if (items[which].equals("Take picture")) {

                Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(i, capture);
            }
        }
    }).create().show();

}    


@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == pick && resultCode == RESULT_OK) {

            imgeUri = data.getData();
            Log.d("test","pick");

            imp.setImageURI(imgeUri);

            editor.putString("link",String.valueOf(imgeUri));
            //Log.d("test",f);
            editor.commit();
            d=true;

        } else if (requestCode == capture && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            imp.setImageBitmap(imageBitmap);
        }
    }    

But the problem is when selecting an image I save it in shared preference the image appears just fine when I'am in activity when go to another and return back this disappear and the activity show the default image, i know the reason it because every time i return to profile activity this was created again and the boolean variable d was false again. How can i fix that when i must call the get preference.?

Daniel B
  • 3,109
  • 2
  • 33
  • 42
norah
  • 69
  • 1
  • 10

1 Answers1

0

In the "onActivityResult()" method, save the imageUri (or imagePath) obtained to sharedPreferences.

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
SharedPreferences.Editor edit = preferences.edit();
edit.putString(key, value);
edit.apply();

Instead of using boolean, check if sharedPreference value is not a null string.If the sharedPreference value is not a null string, update the imageview with imageUri (or imagePath) from sharedPreference.

For loading image in imageView, i recommend you to use photo loading libraries like Picasso or Glide.

Sonu Sanjeev
  • 944
  • 8
  • 9