2016-08-22 73 views
0

我想用按钮打开一个kivy应用程序,当点击按钮时打开android gallary。 所以我写了代码,你可以看到那里,在qpython,它的工作。 但是,当我想运行它在我的电脑,我得到的错误信息:当编辑为apk时,Kivy Gallary程序不起作用

 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手机上启动它,并启动应用程序。 它打开了,我点击了他的按钮,并且gallary也打开了,但是当我选择一个图像时,程序就崩溃了。

这里是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() 

这里是我的自制GALLARY模块:

#-*-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 

而这里是逻辑的一部分,它说与gallary:

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 : [email protected] 
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 
+0

也发布崩溃logcat。 –

+0

好了,编译后的Apk没有崩溃logcat,但是我可以从pc –

+0

Upps发布一个,很抱歉,我没有新的有一种方法,从运行的Android设备获取错误消息,这里是logicat –

回答

0

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

+1

是这样的? requirements = kivy,android –

+0

我需要在buildozer.spec文件的** android.add_jars **中包含java模块吗? –