1

我正在尝试GBoard在我的应用程序中提供支持。我希望用户能够从 GBoard 中选择 GIF。我的onCommitContent样子是这样的:

@Override
public void onCommitContent(InputContentInfoCompat inputContentInfo, int flags, Bundle opts) {
    try {
        if (inputContentInfo != null){
            if (inputContentInfo.getContentUri() != null){
                Log.v(inputContentInfo.getContentUri().getPath());
            }
            if (inputContentInfo.getLinkUri() != null){
                Log.v(inputContentInfo.getLinkUri().getPath());
            }
            Log.v((String)(inputContentInfo.getDescription().getLabel()));
            imageURI = "content://com.google.android.inputmethod.latin" + inputContentInfo.getContentUri().getPath() + inputContentInfo.getLinkUri().getPath();
        }
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), Uri.parse(imageURI));
        imageView.setImageBitmap(bitmap);
    } catch (Exception ex) {
        Log.v(ex.getMessage());
    }

}

但我收到以下异常。

没有内容提供者:content://com.google.android.inputmethod.latin

请帮忙。

4

1 回答 1

0

您需要实施该方案:

URI 语法图

content://方案IETFBCP35 ”中进行了解释,它指导您查看IANA统一资源标识符 (URI) 方案”,其中解释为:

 

URI 方案 | 模板 | 说明 | 状态 | 参考 | 笔记

内容 | 证明/内容| 内容 | 临时 | 戴夫_泰勒| -

 

该链接将您定向到此信息:

"(最后更新于 2012-09-23)

资源标识符 (RI) 方案名称:内容
状态:临时

方案语法:
content://provider/

方案语义:
访问 Android 内容提供程序。

编码注意事项:
未知,谨慎使用。

使用此方案名称的应用程序/协议:
对 Android 内容提供程序执行查询

互操作性注意事项:
未知,谨慎使用。
可能不适合在公共互联网上公开使用。

安全注意事项:
未知,谨慎使用。

联系人:
注册方:Dave Thaler
方案创建者:开放手机联盟

作者/变更控制者:
注册方或经验证代表
方案创建者的人。请参阅上一个答案。

参考资料:
http ://en.wikipedia.org/wiki/Open_Handset_Alliance,http :
//developer.android.com/guide/topics/providers/content-providers.html

(文件创建于 2012 年 9 月 23 日)”。

更多信息请参考上一个 URL“ Android Developers > Docs > Guides > Content providers ”:

“内容提供者可以帮助应用程序管理对自己存储的、其他应用程序存储的数据的访问,并提供一种与其他应用程序共享数据的方式。它们封装数据,并提供定义数据安全性的机制。内容提供者是标准接口将一个进程中的数据与另一个进程中运行的代码连接起来。实现内容提供者有很多优点。最重要的是,您可以配置内容提供者以允许其他应用程序安全地访问和修改您的应用程序数据...

...

许多其他类依赖于ContentProvider类:

如果您使用这些类中的任何一个,您还需要在应用程序中实现内容提供程序。请注意,在使用同步适配器框架时,您还可以创建存根内容提供程序作为替代方案。有关此主题的更多信息,请参阅创建存根内容提供者

来自:创建存根内容提供者

...

添加存根内容提供程序

要为您的应用程序创建存根内容提供程序,请扩展类ContentProvider并存根它所需的方法。以下代码段向您展示了如何创建存根提供程序:

在科特林:

/*
 * Define an implementation of ContentProvider that stubs out
 * all methods
 */
class StubProvider : ContentProvider() {
    /*
     * Always return true, indicating that the
     * provider loaded correctly.
     */
    override fun onCreate(): Boolean  = true

    /*
     * Return no type for MIME type
     */
    override fun getType(uri: Uri): String?  = null

    /*
     * query() always returns no results
     *
     */
    override fun query(
            uri: Uri,
            projection: Array<String>,
            selection: String,
            selectionArgs: Array<String>,
            sortOrder: String
    ): Cursor?  = null

    /*
     * insert() always returns null (no URI)
     */
    override fun insert(uri: Uri, values: ContentValues): Uri? = null

    /*
     * delete() always returns "no rows affected" (0)
     */
    override fun delete(uri: Uri, selection: String, selectionArgs: Array<String>): Int = 0

    /*
     * update() always returns "no rows affected" (0)
     */
    override fun update(
            uri: Uri,
            values: ContentValues,
            selection: String,
            selectionArgs: Array<String>
    ): Int = 0
}

...

在清单中声明提供者

同步适配器框架通过检查您的应用程序是否在其应用程序清单中声明了提供程序来验证您的应用程序是否具有内容提供程序。要在清单中声明存根提供程序,请添加具有以下属性的 < provider > 元素:

...

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.network.sync.BasicSyncAdapter"
    android:versionCode="1"
    android:versionName="1.0" >
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    ...
    <provider
        android:name="com.example.android.datasync.provider.StubProvider"
        android:authorities="com.example.android.datasync.provider"
        android:exported="false"
        android:syncable="true"/>
    ...
    </application>
</manifest>

请参阅上述 URL 以获取完整文档和此简短答案中未包含的更多链接。

于 2019-03-23T17:37:05.627 回答