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

前端 未结 3 1569
孤独总比滥情好
孤独总比滥情好 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:15

    You can just save a string representation of your URI.

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("imageURI", imageUri.toString()); <-- toString()
    

    Then use the Uri parse method to retrieve it.

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    String imageUriString = settings.getString("imageURI", null);
    Uri imageUri = Uri.parse(imageUriString); <-- parse
    

提交回复
热议问题