android - 使用 _data 字段在 Android 数据库(自定义内容提供程序)中存储文件 - v1.6

标签 android file sqlite android-contentprovider

我一直在与我的 Android 应用程序中的一个问题作斗争。我一直在阅读 Android 文档、我关于 Android (Andriod Pro 2) 的书,并查看了几乎每一个我可以动手但无法弄清楚如何实现 文件存储(图像、二进制数据)的示例) 针对内容提供商公开的 Sqlite 表中的记录

这是我所做的(几乎类似于记事本示例应用程序):

  • 在我的提供商中实现了 ContentProvider。这一切都适用于 CRUD 操作。它在内部使用了一个实现SQLiteOpenHelper(非常标准的东西)
  • 我添加了一个文本类型的 _data
  • 我插入一条记录并取回 Uri
  • 在内容提供者上调用 openOutputStream 并开始写入数据

这完全是根据 Android Pro 2 的书。这是那部分代码的 fragment :

Uri uri = getContentResolver().insert(MyAuthority.CONTENT_URI, contentValues);
OutputStream s = getContentResolver().openOutputStream(uri);

但是我得到以下错误:

11-29 01:07:30.717: WARN/System.err(1490): java.io.FileNotFoundException: No files supported by provider at content://myproviderauthority/podcasts/1

我需要做什么?我读 Android documentation但它们非常模糊:

the field that exposes the data to clients should actually contain a content: URI string.

1)那么字段类型需要是TEXT还是有特殊的content: URI string数据类型?

2) 在插入时我没有 URI。我是否需要在插入后获取 URI,然后更新记录中的该字段?

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) 那么这个字段的类型应该是什么?文本 - 我假设?

The client will call ContentResolver.openInputStream() on the user-facing field holding the URI for the item.

4) 这完全是垃圾!如何在字段上调用 ​​ContentResolver.openInputStream()?您只能在记录中调用它并传递 Uri。

感谢您抽出宝贵时间,希望您能帮助我。我还查看了这些 SO 问题(thisthis),但它们没有帮助。

顺便说一句,我是针对 API 的第 4 级(版本 1.6)构建的。

最佳答案

要查看“提供商不支持任何文件...”的来源,请查看 here .那里的情况似乎与您描述的情况非常相似。

简而言之,您的内容提供者(或其某些父类(super class))需要覆盖并实现openFile 方法。该错误是由默认的“未实现——退后!”引发的。 ContentProvider.java 中的代码。

我发现在遇到困难时查看框架的底层源代码很有帮助,因为文档有时会有点缺乏。

关于android - 使用 _data 字段在 Android 数据库(自定义内容提供程序)中存储文件 - v1.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4307238/

相关文章:

java - 使用 .. 父目录说明符 ("dot dot"解析路径名)而不解析符号链接(symbolic link)

android - SQLiteOpenHelper onCreate()/onUpgrade() 何时运行?

PHP 对数组中的多个变量运行 1 个查询

java - 从 Activity 中访问应用程序广播接收器?

java - "Make sure to call FirebaseApp.initializeApp(Context) first."如何解决这个错误?

c++ - FSCTL_GET_RETRIEVAL_POINTERS 对小文件失败

objective-c - 在iOS中使用SQLite混合逻辑运算符OR和AND

android - SharedTest 在 Android Studio Chipmunk 上收到警告 "Duplicate content root detected"

java - 向 GCM 服务器发送消息(使用 Java)时出现异常 [errorCode=MismatchSenderId]

c++ - 在C++中查找文件中的特定文件