Android development template code (2) -- sets an image for ImageView. After exiting, The ImageView status can be saved,
Then select the image from the image library and set it to ImageView. However, I found a problem, that is, when the image view enters the default state again, after searching for the information for a long time, I finally found a solution. Use SharePreference to store the data.
Paste code
Private void save (String imagePath) {SharedPreferences. editor editor = getSharedPreferences ("data", MODE_PRIVATE ). edit (); // get SHaredPreferences. editor object editor. putBoolean ("imageChange", true); // Add a boolean value named imageChange. The value is true editor. putString ("imagePath", imagePath); // Save the imagePath image path editor. apply (); // submit} private void load () {SharedPreferences preferences = getSharedPreferences ("data", MODE_PRIVATE ); // get the SharedPreferences object // the judgment in the brackets is to find the corresponding value of imageChange. If it cannot be found, false is returned. If it is found, true is defined above, the statement if (preferences. getBoolean ("imageChange", false) {String imagePath = preferences. getString ("imagePath", ""); // retrieves the saved imagePath. If the saved imagePath cannot be found, a blank displayImage (imagePath) is returned. // call the image display method, set images for ImageView }}
Use
Save the image path
Load () is placed in the onCreateed method, after Initialization
Test:
Logical Analysis
Download Demo