kivy android共享图片 [英] kivy android share image

查看:133
本文介绍了kivy android共享图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create share button that will use android ACTION_SEND intent for sharing image. It's my code:

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)

但是它不起作用)它在此行shareIntent.putExtra(Intent.EXTRA_STREAM, uri)中引发此错误jnius.jnius.JavaException: Invalid instance of u'android/net/Uri' passed for a u'java/lang/String'.我该如何解决?

But it doesn't work) It throws this error jnius.jnius.JavaException: Invalid instance of u'android/net/Uri' passed for a u'java/lang/String' in this line shareIntent.putExtra(Intent.EXTRA_STREAM, uri). How can i fix this?

推荐答案

我找到了解决方案.您必须将uri转换为可打包的,然后将其传递给intent:

I found solution. You must cast uri to parcelable and then pass it to intent:

parcelable = cast('android.os.Parcelable', uri)
shareIntent.putExtra(Intent.EXTRA_STREAM, parcelable)

这篇关于kivy android共享图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆