Domanda

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));

        }


    }
}
È stato utile?

Soluzione

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));
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top