【问题标题】:Kivy Gallary programm doesn't work when compiled as apkKivy Gallery 程序在编译为 apk 时不起作用
【发布时间】:2016-12-28 16:45:16
【问题描述】:

我想制作一个带有按钮的 kivy 应用程序,当单击按钮时,它会打开 android 库。 所以我写了代码,你可以在那里看到,在 qpython 上,它起作用了。 但是当我想在我的电脑上运行它时,我收到了错误消息:

     Traceback (most recent call last):
   File "main.py", line 35, in <module>
     from gallery import Gallary
   File "/home/gilgamesch/Apps/event/gallery.py", line 14, in <module>
     from android import activity
 ImportError: No module named android

(是的,这是整个错误信息)

但我还是决定试一试。 所以我用buildozer编译它,在我的android手机上安装并启动它,应用程序就启动了。 它打开了,我点击了他的按钮,画廊也打开了,但是当我选择一张图片时,程序就崩溃了。

这里是main.py:

#-*-coding:utf8;-*-
#qpy:2
#qpy:kivy

from kivy.app import App
from kivy.uix.button import Button
from kivy.properties import StringProperty

from gallery import Gallary


class TestApp(App):
    path = StringProperty()
    def build(self):


        return Button(text='Hello QPython', on_release=self.open_g)
    def open_g(self, *args):
        self.g = Gallary()

        path = self.g.make_gallary(self.getstr)

    def getstr(self, path):
        self.path = path
        print self.path + 'in Gallary class'

    def on_pause(self):
        return True

    def on_resume(self):
        pass


TestApp().run()

这是我自制的图库模块:

#-*-coding:utf8;-*-
#qpy:2
#qpy:kivy

from jnius import autoclass
from jnius import cast

from android import activity


class Gallary(object):
    def __init__(self, *args):

        self.mypath = ''

        # import the needed Java class 
        self.PythonActivity = autoclass ('org.renpy.android.PythonActivity')
        self.Activity = autoclass('android.app.Activity')

        self.uri = autoclass('android.net.Uri')
        self.intent = autoclass('android.content.Intent')
        self.MediaStore = autoclass('android.provider.MediaStore')
        self.Cursor = autoclass('android.database.Cursor')


        # Value of MediaStore.Images.Media.DATA
        self.MediaStore_Images_Media_DATA = "_data"



        self.gallerie = self.intent()
        self.gallerie.setType("image/*")
        self.gallerie.setAction(self.intent.ACTION_GET_CONTENT)



    def getPath(self, Iuri, *args):
        if Iuri == None:
            return ''
        self.projektion = [self.MediaStore_Images_Media_DATA]

        self.cursor = self.currentActivity.getContentResolver().query(Iuri, self.projektion, None, None, None)
        if self.cursor != None:
            cindex = self.cursor.getColumnIndexOrThrow(self.MediaStore_Images_Media_DATA)
            self.cursor.moveToFirst()
            return self.cursor.getString(cindex)
        return Iuri.getPath()

    def on_activity_result(self, requestCode, resultCode, data):

        print "### ACTIVITY CALLBACK ###"
        if resultCode == self.PythonActivity.RESULT_OK:
            if requestCode == 1:

                myuri = data.getData()
                self.mypath = self.getPath(myuri)
                self.give(self.mypath)
                #print self.mypath

    def make_gallary(self, give):

        self.currentActivity = cast('android.app.Activity', self.PythonActivity.mActivity)
        self.currentActivity.startActivityForResult(self.intent.createChooser(self.gallerie, "Choose Image"), 1)
        #print 'made gallery'
        self.give = give

        activity.bind(on_activity_result=self.on_activity_result)

        return self.mypath

    def get_str(self):
        return self.mypath
    def give(self):
     pass

这里是权限,我在 buildozer.spec 文件中给了应用程序:

# (list) Permissions
android.permissions = INTERNET,ACCESS_WIFI_STATE,READ_PHONE_STATE,ACCESS_NETWORK_STATE,READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE

这是逻辑的一部分,它用galary表示:

D/GalleryActivityLifecycleCallback(15128): destroyed : count 1
D/ThumbnailProxy(15128): stop()
D/ThumbnailProxy(15128): stop()
D/ThumbnailProxy(15128): stop()
D/KeyguardUpdateMonitor( 1015): sendKeyguardVisibilityChanged(true)
D/KeyguardUpdateMonitor( 1015): handleKeyguardVisibilityChanged(1)
D/CustomFrequencyManagerService(  785): acquireDVFSLockLocked : type : DVFS_MIN_LIMIT  frequency : 1190400  uid : 1000  pid : 785  pkgName : ACTIVITY_RESUME_BOOSTER@9
D/NearbyUtils(15128): clear nearby caches
E/cutils  (  213): Failed to mkdirat(/storage/extSdCard/Android): Read-only file system
W/Vold    (  213): Returning OperationFailed - no handler for errno 30
D/KeyguardUpdateMonitor( 1015): sendKeyguardVisibilityChanged(true)
D/KeyguardUpdateMonitor( 1015): handleKeyguardVisibilityChanged(1)
W/ContextImpl(15128): Failed to ensure directory: /storage/extSdCard/Android/data/com.sec.android.gallery3d/cache
I/Gallery_Performance(15128): Gallery onDestroy End

【问题讨论】:

  • 也发布崩溃日志。
  • 嗯,编译后的Apk没有崩溃logcat,但我可以从电脑上发布一个
  • Upps,对不起,我没有新方法,从正在运行的 android 设备获取错误消息,这里是 logicat

标签: android python kivy android-gallery pyjnius


【解决方案1】:

尝试将android 添加到您的要求中。

【讨论】:

  • 这样吗?要求 = kivy,android
  • 我需要在 buildozer.spec 文件中包含 android.add_jars 中的 java 模块吗?
【解决方案2】:

无需添加,android.add_jars 在 buildozer.spec 文件中 要求 = kivy,android ,pyjnius

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2019-09-19
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 2022-09-28
  • 1970-01-01
  • 1970-01-01
  • 2011-06-25
  • 2022-08-21
相关资源
最近更新 更多