如何使用SharedPreferences保存URI或任何存储? [英] How to use SharedPreferences to save a URI, or any Storage?
问题描述
URI imageUri = null;
//Setting the Uri of aURL to imageUri.
try {
imageUri = aURL.toURI();
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
我正在使用此代码将URL转换为URI.如何将imageUri保存到SharedPreferences或无法在onDestroy()上将其删除的内存中?
I am using this code to translate a URL to a URI. How could I save the imageUri to a SharedPreferences, or a memory where it wouldnt be deleted onDestroy()?
我不想做SQLite数据库,因为URL更改时URI会改变.我不想用完未使用的URI的内存
I dont want to do SQLite database because the URI will change when the URL's change.I dont want to use up memory for unused URI's
推荐答案
要开始使用SharedPreferences进行存储,您需要在onCreate()中添加如下内容:
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();
}
这篇关于如何使用SharedPreferences保存URI或任何存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!