我想制作一个带有按钮的kivy应用程序,当点击按钮时打开android gallary。 所以我写了代码,你可以在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手机上启动它,应用程序启动了。 它打开了,我点击了他的按钮,并且也打开了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
以下是逻辑分析的一部分,其中包含了一些内容:
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
答案 0 :(得分:0)
尝试将android
添加到您的要求中。