简体   繁体   English

Android:将android.net.Uri对象保存到数据库

[英]Android: Save android.net.Uri object to Database

What im trying to do is get the selected ringtone from the user, set an AlarmManager alarm to play that ringtone when the alarm goes Off. 我想要做的是从用户那里获取所选的铃声,设置一个AlarmManager闹钟,以在闹钟关闭时播放该铃声。 but I need to save the ringtone in the database so I can reset all the alarms after phone reboot. 但我需要将铃声保存在数据库中,这样我可以在手机重启后重置所有闹钟。

my question is what is the best way to save the ringtone Uri to the database to retrieve later? 我的问题是什么是将铃声Uri保存到数据库以便以后检索的最佳方法?

I tried the followings: 我尝试了以下几点:

1) save the ringtone title in the DB and then retrieve it and append it to a default ringtone path. 1)将铃声标题保存在数据库中,然后检索它并将其附加到默认铃声路径。 but the issue, is that the ringtone might be loaded from a different location 2) Storing the uri scheme, scheme spefici part and fragment and then call Uri.fromParts to create the Uri. 但问题是,铃声可能是从不同的位置加载2)存储uri方案,方案spefici部分和片段,然后调用Uri.fromParts来创建Uri。 3) create an inputStream, byte[] array from the Uri and saving it as a blob and then reading it back and cast the result to Uri 3)从Uri创建一个inputStream,byte []数组并将其保存为blob,然后将其读回并将结果转换为Uri

none of these works. 这些都不起作用。

Your help is much appreciated. 非常感谢您的帮助。

Store the URI as a string in the database and then load it later. 将URI作为字符串存储在数据库中,然后再加载它。

// This will get the uri in a string format
String s = mUri.toString();

When you retrieve the string from the database, rebuild the URI like this: 从数据库中检索字符串时,重建URI如下所示:

// This will decode the string into a URI
Uri mUri = Uri.parse(s);

Hope that helps. 希望有所帮助。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM