lulu
lulu

Reputation: 7

My app keeps resetting after I close it

My app keeps resetting after I close it.

public class MainActivity extends AppCompatActivity {
    public static final int FILE_REQUEST_CODE = 1;
    ImageView IMG ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IMG = (ImageView) findViewById(R.id.imageView1);
        Button pickBtn = (Button) findViewById(R.id.pickBtn);

        pickBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pickImg();
            }
        });

    }

    private void pickImg() {
        new MaterialFilePicker()
                .withActivity(this)
                .withRequestCode(1)
                .withFilter(Pattern.compile(".*\\.(jpg|jpeg|png)$"))
                .withHiddenFiles(true)
                .withTitle("Sample title")
                .start();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == FILE_REQUEST_CODE && resultCode == RESULT_OK) {
            String filePath = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
            // Do anything with file
            File imgFile = new  File(filePath);
            if(imgFile.exists()){
                Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                IMG.setImageBitmap(myBitmap);
            }
        }
    }
}

My app is only filled with one button and one image default. With the button I can pick an image to replace the default image.

But when I close the app and open it again the image is back to default.

How to make it show the last picked image when I start the app??

Upvotes: 1

Views: 44

Answers (1)

user5718327
user5718327

Reputation:

If this app is performing simple and offline task, you could use Shared Preference to store the location of the Image. When onCreate, check whether the sharedpreference object is null or not, if not, load with the value stored.

Upvotes: 1

Related Questions