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.

Here is my code at the moment on main.java. It shows how i have used code to chose a photo from gallary butneed to save that photo:

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


public class Main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

{
}

        Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
        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);
            }
        });;


            Button week1 = (Button) findViewById(R.id.week1button);
            Button week2 = (Button) findViewById(R.id.week2button);
        //Button Sound
        final MediaPlayer buttonSound = MediaPlayer.create(Main.this, R.raw.sound1);


        week1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                buttonSound.start();
                startActivity(new Intent("com.example.timetable.WEEK1"));
            }});

                week2.setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        buttonSound.start();
                        startActivity(new Intent("com.example.timetable.WEEK2"));
                    }
        });       

          };

    @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;
    }




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 && resultCode == RESULT_OK && 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));

        }


    }
}
vortexwolf
  • 13,967
  • 2
  • 54
  • 72
user3195144
  • 23
  • 1
  • 6
  • Eclipse is just the IDE, rather mention the platform in the title instead. – FD_ Feb 16 '14 at 13:21
  • 2
    Duplicate question - http://stackoverflow.com/questions/8586242/how-can-i-store-images-using-sharedpreference-in-android – AndyFaizan Feb 16 '14 at 13:52
  • Definitely a duplicate. OP has chosen a delusive title and hasn't found out that such questions were already asked. – vortexwolf Feb 16 '14 at 15:08

1 Answers1

2

Save image path in SharedPreference

SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit=shre.edit();
edit.putString("profilePic", picturePath);
edit.commit();

When activity start next time, check your SharedPreference. If exist, set image. The following codes may write in onCreate().

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));
}
Thein
  • 3,940
  • 2
  • 30
  • 34
  • I had to create a felid for picturePath and it won't let me use .getstring where it says profilePic, ture. What should i do? – user3195144 Feb 16 '14 at 15:52
  • I got it to work, many thanks. I thought i had the second part in my oncreate, but obviuosly didnt. I moved it and now works. Thanks Again. – user3195144 Feb 16 '14 at 16:24