android中基于文件的文档存储
我正处于 Android 笔记应用程序的早期阶段,我希望有人能给我指出一个存储笔记数据的好解决方案。
理想情况下,我希望有一个解决方案,其中:
- 每个笔记文档都是一个单独的文件(用于保管箱同步)
- 笔记可以由多个页面组成
- 笔记页面可以包含二进制数据(例如图像)
- 可以加载单个页面而无需必须将整个文档解析到内存中
- 线程安全:可以同时发生多个读/写。
XML 已经退出(至少对于整个文件),因为我没有很好的方法来一次提取单个页面。我考虑过使用 zip 文件,但是(尤其是压缩时)我认为它们也会被卡在加载整个文件上。
似乎应该有一个 Java 库可以做到这一点,但我的 google-fu 失败了。我能想到的唯一的其他选择是为每个笔记创建一个单独的 sqlite 数据库。
有人知道解决这个问题的好方法吗?谢谢!
I'm in the early stages of a note-taking application for android and I'm hoping that somebody can point me to a nice solution for storing the note data.
Ideally, I'm looking to have a solution where:
- Each note document is a separate file (for dropbox syncing)
- A note can be composed of multiple pages
- Note pages can have binary data (such as images)
- A single page can be loaded without having to parse the entire document into memory
- Thread-safety: Multiple reads/writes can occur at the same time.
XML is out (at least for the entire file), since I don't have a good way to extract a single page at a time. I considered using zip files, but (especially when compressed) I think they'd be stuck loading the entire file as well.
It seems like there should be a Java library out there that does this, but my google-fu is failing me. The only other alternative I can think of is to make a separate sqlite database for every note.
Does anybody know of a good solution to this problem? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来关系数据库可以在这里工作。您只需要稍微尝试一下该模式即可。
也许创建一个页面表,其中每个页面都包含一个用于其所属文档的字段以及一个用于其在文档中的顺序的字段。页面还可以有一个二进制数据字段,该字段可能包含在另一个表中。如果文档本身有附加数据,也许您也有一个文档表。
我还没有在 Android 设备上使用过 SQLite 事务,但这似乎是解决线程安全问题的好方法。
Seems like a relational database would work here. You just need to play around with the schema a little.
Maybe make a Pages table with each page including, say, a field for the document it belongs to and a field for its order in the document. Pages could also have a field for binary data, which might be contained in another table. If the document itself has additional data, maybe you have a table for documents too.
I haven't used SQLite transactions on an Android device, but it seems like that would be a good way to address thread safety.
我建议使用 SQLite 来存储文档。最终,这比每次访问笔记时都尝试处理文件 I/O 更容易。然后,当有人想要上传到保管箱时,您可以动态生成文件并上传。至少有一个Notes表和一个pages表是有意义的。这样您就可以单独加载每个页面,而注释只是页面的集合。此外,您还可以将特定页面的图像作为 BLOBS 存储在数据库中。基本上,如果您只需要每页一种类型的内容,那么您将在页面表中包含诸如 id 列和内容列之类的内容。或者,如果您想支持更复杂的内容(例如多种类型的内容),那么您需要使页面成为其他内容的集合,例如“实体”。
IMO,关系数据库将是满足从特定页面读取而无需加载整个文件的要求的最简单方法。
I would recommend using SQLite to store the documents. Ultimately, it'll be easier than trying to deal with file I/O every time you access the note. Then, when somebody wants to upload to dropbox, you generate the file on the fly and upload it. It would make sense to have a Notes table and a pages table, at least. That way you can load each page individually and a note is just a collection of pages anyway. Additionally, you can store images as BLOBS in the database for a particular page. Basically, if you only want one type of content per page, then you would have, in the pages table, something like an id column and a content column. Alternatively, if you wanted to support something that is more complex such as multiple types of content then you would need to make your pages a collection of something else, like "entities."
IMO, a relational database is going to be the easiest way to accomplish your requirement of reading from particular pages without having to load the entire file.