【问题标题】:Image keyboard : selecting rich content(Gif, Stickers) reopens activity图像键盘:选择丰富的内容(Gif、贴纸)重新打开活动
【发布时间】:2019-10-24 18:53:28
【问题描述】:

我正在尝试在我的应用中实现图像键盘支持。我关注了official documentation。为了支持这一点,我需要覆盖 EditTextonCreateInputConnection 来告诉软键盘什么应用程序支持和一个回调来获取选定的内容 Uri。

编辑文本:

override fun onCreateInputConnection(editorInfo: EditorInfo): InputConnection {
    val ic: InputConnection = super.onCreateInputConnection(editorInfo)
    EditorInfoCompat.setContentMimeTypes(editorInfo, arrayOf("image/jpeg", "image/png"))

    val callback =
        InputConnectionCompat.OnCommitContentListener { inputContentInfo, flags, opts ->
            val lacksPermission = (flags and
                    InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0
            // read and display inputContentInfo asynchronously
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 && lacksPermission) {
                try {
                    inputContentInfo.requestPermission()
                } catch (e: Exception) {
                    return@OnCommitContentListener false // return false if failed
                }
            }

            // read and display inputContentInfo asynchronously.
            // call inputContentInfo.releasePermission() as needed.

            true  // return true if succeeded
        }
    return InputConnectionCompat.createWrapper(ic, editorInfo, callback)
}

它工作正常。惊喜!!!

问题是当我将意图过滤器添加到活动时。添加意图过滤器回调后,InputConnectionCompat.OnCommitContentListener 不再调用,它使用支持的意图过滤器打开活动。

清单:

<activity
            android:name=".Main2Activity"
            android:label="@string/title_activity_main2">

        <intent-filter> <-- this filter is added
            <action android:name="android.intent.action.SEND"/>

            <category android:name="android.intent.category.DEFAULT"/>

            <data android:mimeType="image/*"/>
        </intent-filter>
    </activity>

样品在github 提前致谢。

【问题讨论】:

    标签: android android-edittext android-softkeyboard gboard


    【解决方案1】:

    不知道为什么和如何,但从改变

    EditorInfoCompat.setContentMimeTypes(editorInfo, arrayOf("image/jpeg", "image/png"))
    

    EditorInfoCompat.setContentMimeTypes(editorInfo, arrayOf("image/*"))
    

    解决了问题

    【讨论】:

    • 它为 editorInfo 传递了错误的 mime 类型。它被设置为 jpeg 或 png,而选定的图像在 gifs/stickers 中。现在可以选择任何类型的图像。
    猜你喜欢
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 2011-06-14
    • 2021-04-25
    • 1970-01-01
    相关资源
    最近更新 更多