android – 如何使用SharedPreferences保存URI或任何存储?

android – 如何使用SharedPreferences保存URI或任何存储?,第1张

概述URI imageUri = null;//Setting the Uri of aURL to imageUri.try { imageUri = aURL.toURI();} catch (URISyntaxException e1) { // TODO Auto-generated catch block e1.printStackTrace();} 我正在
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()?

我不想做sqlite数据库,因为当URL改变时URI会改变.我不想用掉未使用的URI的内存

解决方法 要开始使用SharedPreferences进行存储,您需要在onCreate()中使用类似的内容:
SharedPreferences myPrefs = getSharedPreferences(mytag,0);SharedPreferences.Editor myPrefsEdit = myPrefs.edit();

我认为你可以做这样的事情来存储它:

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

然后这样的东西来检索:

try {    imageUri = URI.create(myPrefs.getString("url","defaultString"));} catch (IllegalArgumentException e) {    // Todo auto-generated catch block    e.printstacktrace();}
总结

以上是内存溢出为你收集整理的android – 如何使用SharedPreferences保存URI或任何存储?全部内容,希望文章能够帮你解决android – 如何使用SharedPreferences保存URI或任何存储?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1134645.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存