What I want to achieve:
I'm working on a small APP that can stores images and videos on the 'on_disk' sqlite3 database.
I would like to:
- Retrieve the images and videos without internet connection
What I have done:
I did some researches about retrieving images from sqlite in kivy. For example I came across this post Retrieve Image from sqlite3 database and directly display on kivy window. However, I tried the same thing, but I couldn't achieve my goal.
My Code example:
videoandsql.py
import kivy
from kivy.app import App
from kivy.uix.videoplayer import VideoPlayer
from kivy.uix.boxlayout import BoxLayout
import sqlite3 as lite, StringIO, sys
class VideoAndSql(BoxLayout):
def __init__(self, *args, **kwargs):
super(VideoAndSql, self).__init__(*args, **kwargs)
def readImage():
try:
fin = open("image5.jpg", "rb")
imag = fin.read()
return(imag)
except IOError, e:
print "Error %d: %s" % (e.args[0],e.args[1])
sys.exit(1)
finally:
if fin:
fin.close()
try:
con = lite.connect('test.db')
cur = con.cursor()
data = readImage()
binary = lite.Binary(data)
cur.execute("INSERT INTO Images(Data) VALUES (?)", (binary,) )
con.commit()
except lite.Error, e:
if con:
con.rollback()
print "Error %s:" % e.args[0]
sys.exit(1)
finally:
if con:
con.close()
def writeImage(data):
try:
pic = open('picture.jpg','wb')
pic.write(data)
except IOError, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit(1)
finally:
if pic:
pic.close()
try:
con = lite.connect('test.db')
cur = con.cursor()
cur.execute("SELECT Data FROM Images LIMIT 1")
data = cur.fetchone()[0]
return (data)
writeImage(data)
except lite.Error, e:
print "Error %s:" % e.args[0]
sys.exit(1)
finally:
if con:
con.close()
def videoTest(self, *args):
return VideoPlayer(source ="mm.mp4")
class VideoAndSqlTest(App):
def build(self):
return VideoAndSql()
VideoAndSqlTest().run()
videoandsqltest.kv
<VideoAndSql>:
BoxLayout:
spacing: 10
orientation: "vertical"
Label:
text: "Sql and Video testing app"
Image:
source: root.writeImage()
Button:
text: "sql"
Button:
text: "video"
#on_release: root.videoTest()
VideoPlayer:
source: "mm.mp4"
Observation:
I can copy the image from the sqlite3 database to a file on my system and view it using windows viewer but I can't display it on my kivy app.
Suggestions:
-
Is there a better way I could do this?
-
Is it possible to use any of the kivy's storages (json, dict store, redis db) to achieve what I want? If yes, Is it simplier? Can anyone give me a sample code please?
-
I'll appreciate any suggestion(s) and answer(s) that can help me to resolve this and get what I want to achieve.
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire