1

我正在尝试在我的应用程序中实现图像键盘支持。我遵循了官方文档。为了支持这一点,我需要重写EditText'sonCreateInputConnection来告诉软键盘什么应用程序支持和一个回调来获取选定的内容 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 上提前谢谢。

4

1 回答 1

1

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

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

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

解决了问题

于 2019-06-11T05:39:49.747 回答