How to use SharedPreferences to save a URI, or any Storage?

前端 未结 3 1554
孤独总比滥情好
孤独总比滥情好 2021-01-02 14:19
URI imageUri = null;

//Setting the Uri of aURL to imageUri.
try {
    imageUri = aURL.toURI();
} catch (URISyntaxException e1) {
    // TODO Auto-generated catch bl         


        
3条回答
  •  借酒劲吻你
    2021-01-02 15:05

    To get started using SharedPreferences for storage you'll need to have something like this in your onCreate():

    SharedPreferences myPrefs = getSharedPreferences(myTag, 0);
    SharedPreferences.Editor myPrefsEdit = myPrefs.edit();
    

    I think you can do something like this to store it:

    myPrefsEdit.putString("url", imageUri.toString());
    myPrefsEdit.commit();
    

    And then something like this to retrieve:

    try {
        imageUri = URI.create(myPrefs.getString("url", "defaultString"));
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

提交回复
热议问题