使用_data场的Android数据库(自定义内容提供商)存储的文件 - V1.6 [英] Storing file in Android Database (Custom Content Provider) using _data field - v1.6
问题描述
我一直缠斗在我的Android应用程序的问题。我一直在读Android的文档,我的书在Android(安卓临2),并看着几乎每一个例子,我可以得到我的手,但无法弄清楚如何实施文件的存储(图像,二进制数据)对由内容提供者暴露在SQLite表记录
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):
- 在我的供应商实施
的ContentProvider
。它所有的工作进行CRUD操作。在内部,它使用一个执行SQLiteOpenHelper
(pretty的标准的东西) - 在我添加类型文本
_data code>列
- 在我插入一条记录,并得到URI回
- 呼叫openOutputStream上的内容提供商,并开始将数据写入
- 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
根据Android的临2 book.Here这恰恰是code这部分的片段:
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:
1月十一日至29日:07:30.717: WARN / System.err的(1490): java.io.FileNotFoundException:无 在文件支持的供应商 内容:// myproviderauthority /播客/ 1
11-29 01:07:30.717: WARN/System.err(1490): java.io.FileNotFoundException: No files supported by provider at content://myproviderauthority/podcasts/1
什么是我需要做什么?我读的Android 文档但他们都非常含糊:
What do I need to do? I read Android documentation but they are very vague:
这暴露了数据字段 客户实际上应该包含一个 内容:URI字符串
the field that exposes the data to clients should actually contain a content: URI string.
1),那么请问字段类型必须是文本还是有一个特殊的内容:URI字符串
数据类型
1) So does the field type need to be TEXT or is there a special content: URI string
data type?
2)在插入我没有URI的时间。我需要得到URI插入后,然后更新记录中的那场?
该记录还应该有另一个 现场,名为_data,列出了 该设备适用于以确切的文件路径 文件。该字段不旨在是 由客户机读的,而是由 ContentResolver的。
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)那么,应该是这个字段的类型是什么?文本 - ?我认为
客户端将调用 ContentResolver.openInputStream上() 用户面向字段保持该URI 为项
The client will call ContentResolver.openInputStream() on the user-facing field holding the URI for the item.
4)这是绝对的垃圾!我怎么能叫 ContentResolver.openInputStream()
在球场上?你只能把这种记录在案并通过URI。
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.
感谢您的时间,我希望你能帮助我。我还审查了这些SO问题(<一href="http://stackoverflow.com/questions/3883211/how-to-store-large-blobs-in-an-android-content-provider">this和<一href="http://stackoverflow.com/questions/3926553/getcontentresolver-openoutputstreamuri-no-files-supported-by-provider">this)他们并没有帮助。
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.
顺便说一句,我对建筑的API等级4(1.6版本)。的
推荐答案
要看到无由供应商在...支持的文件的来源,看看<一href="http://stackoverflow.com/questions/3883211/how-to-store-large-blobs-in-an-android-content-provider/4336013#4336013">here.这种情况似乎与你所描述的非常相似。
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.
总之,你的内容提供商(或它的一些超)需要重写和实施中openFile
方法。这引发错误由默认的未实现 - 后退! code。在 ContentProvider.java
。
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 in ContentProvider.java
.
我发现它有助于在底层源$ C $构架了C偷看当我遇到问题,因为文件有时有点欠缺。
I find it helpful to peek at underlying source code of the framework when I get stuck, since documentation is sometimes a bit lacking.
这篇关于使用_data场的Android数据库(自定义内容提供商)存储的文件 - V1.6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!