kivy android 分享图片

Dim*_*osh 7 python android kivy pyjnius

我想创建共享按钮,该按钮将使用 android ACTION_SEND 意图来共享图像。这是我的代码:

from kivy.setupconfig import USE_SDL2


def share(path):
    if platform == 'android':
        from jnius import cast
        from jnius import autoclass
        if USE_SDL2:
            PythonActivity = autoclass('org.kivy.android.PythonActivity')
        else:
            PythonActivity = autoclass('org.renpy.android.PythonActivity')
        Intent = autoclass('android.content.Intent')
        String = autoclass('java.lang.String')
        Uri = autoclass('android.net.Uri')
        File = autoclass('java.io.File')

        shareIntent = Intent(Intent.ACTION_SEND)
        shareIntent.setType('"image/*"')
        imageFile = File(path)
        uri = Uri.fromFile(imageFile)
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri)

        currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
        currentActivity.startActivity(shareIntent)
Run Code Online (Sandbox Code Playgroud)

但它不起作用)它jnius.jnius.JavaException: Invalid instance of u'android/net/Uri' passed for a u'java/lang/String'在这一行中引发了这个错误shareIntent.putExtra(Intent.EXTRA_STREAM, uri)。我怎样才能解决这个问题?

Dim*_*osh 7

我找到了解决方案。您必须将 uri 转换为parcelable,然后将其传递给意图:

parcelable = cast('android.os.Parcelable', uri)
shareIntent.putExtra(Intent.EXTRA_STREAM, parcelable)
Run Code Online (Sandbox Code Playgroud)