使用 _data 字段在 Android 数据库(自定义内容提供程序)中存储文件 - v1.6
我一直在与我的 Android 应用程序中的一个问题作斗争。我一直在阅读 Android 文档,我关于 Android 的书(Andriod Pro 2),并且查看了几乎每一个我能接触到的示例,但无法弄清楚如何实现文件(图像、二进制数据)的存储)针对内容提供商公开的 Sqlite 表中的记录。
这是我所做的(几乎类似于记事本示例应用程序):
- 在我的提供程序中实现了
ContentProvider
。这一切都适用于 CRUD 操作。在内部它使用一个实现SQLiteOpenHelper
(相当标准的东西) - 我添加了一个文本类型的
_data
列 - 我插入一条记录并获取Uri返回
- 在内容提供者上调用openOutputStream并开始写入数据
这完全是根据Android Pro 2书。这是该部分代码的片段:
Uri uri = getContentResolver().insert(MyAuthority.CONTENT_URI, contentValues);
OutputStream s = getContentResolver().openOutputStream(uri);
但是我收到以下错误:
11-29 01:07:30.717: 警告/系统错误(1490): java.io.FileNotFoundException:否 提供者支持的文件位于 内容://myproviderauthority/podcasts/1
我需要做什么?我阅读了 Android 文档 但它们非常模糊:
向其公开数据的字段 客户端实际上应该包含一个 内容:URI 字符串。
1) 那么字段类型是否需要为 TEXT 还是有特殊的 content: URI string
数据类型?
2) 在插入时我不有 URI。我是否需要在插入后获取 URI,然后更新记录中的该字段?
该记录还应该有另一个 名为“_data”的字段,列出了 设备上的确切文件路径 文件。该字段的目的不是 由客户阅读,但由 内容解析器。
3)那么这个字段的类型应该是什么?文本 - 我想?
客户端会调用 ContentResolver.openInputStream() 上 保存 URI 的面向用户的字段 对于该项目。
4)这完全是垃圾!如何在字段上调用 ContentResolver.openInputStream()
?您只能在记录中调用此内容并传递 Uri。
感谢您的宝贵时间,希望您能帮助我。我还审查了这些问题(这个 和这个),他们没有帮助。
顺便说一句,我正在针对 API 的 4 级(版本 1.6)进行构建。
I have been battling with an issue in my Android app. I have been reading Android docs, my book on Android (Andriod Pro 2) and have looked at almost each and every example I could get my hands on but could not figure out how to implement storing of files (images, binary data) against records in Sqlite table exposed by the content provider.
Here is what I have done (almost similar to Notepad sample app):
- Implemented
ContentProvider
in my provider. It all works for CRUD operations. Internally it uses an implementationSQLiteOpenHelper
(pretty standard stuff) - I have added a
_data
column of type text - I insert a record and get the Uri back
- Call openOutputStream on the content provider and start writing the data
This is exactly according to the Android Pro 2 book.Here is the snippet of that part of code:
Uri uri = getContentResolver().insert(MyAuthority.CONTENT_URI, contentValues);
OutputStream s = getContentResolver().openOutputStream(uri);
But I get the error below:
11-29 01:07:30.717:
WARN/System.err(1490):
java.io.FileNotFoundException: No
files supported by provider at
content://myproviderauthority/podcasts/1
What do I need to do? I read Android documentation but they are very vague:
the field that exposes the data to
clients should actually contain a
content: URI string.
1) So does the field type need to be TEXT or is there a special content: URI string
data type?
2) At the time of inserting I do not have the URI. Do I need to get the URI after insert and then update that field in the record?
The record should also have another
field, named "_data" that lists the
exact file path on the device for that
file. This field is not intended to be
read by the client, but by the
ContentResolver.
3) So what should be the type of this field be? TEXT - I assume?
The client will call
ContentResolver.openInputStream() on
the user-facing field holding the URI
for the item.
4) This is utter rubbish! How can I call ContentResolver.openInputStream()
on the field?? You can only call this on the record and passing the Uri.
Thank you for your time and I hope you can help me. I have also reviewed these SO questions (this and this) and they did not help.
By the way I am building against Level 4 of the API (version 1.6).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要了解“提供商不支持...”的来源,请查看 此处。那里的情况似乎与你所描述的非常相似。
简而言之,您的内容提供程序(或其某些超类)需要重写并实现
openFile
方法。该错误是由默认的“未实现——退出!”引发的。ContentProvider.java
中的代码。我发现当我遇到困难时查看框架的底层源代码很有帮助,因为文档有时有点缺乏。
To see where "No files supported by provider at..." comes from, have a look here. The situation there seems very similar with the one you've described.
In short, your content provider (or some of its superclasses) needs to override and implement
openFile
method. That error is raised by the default "not implemented--back off!" code inContentProvider.java
.I find it helpful to peek at underlying source code of the framework when I get stuck, since documentation is sometimes a bit lacking.