How to include Internet Permission in Kivy? (Android 6) - android

I want to use the standard Python module urllib2 in my Kivy App which requires access to the Internet. But when I uncomment #android.permissions = INTERNET
and package the App the App doesn't inform me about the Internet Permission while installing the .apk and when I'm using urllib2 my App crashes (probably because no Internet Permission)
Thx in advance!

In this case the modification is not made in the python code but in the buildozer.spec file.
Inside this file on line 87 you will find "# (list) Permissions" on the line below it will have "# android.permissions = INTERNET" uncomment this line so that the app has internet access permission, if you don't find the line you can copy and paste "android.permissions = INTERNET", this way the apk will be able to connect to the internet, but it may end up with a certificate error "SSL: CERTIFICATE_VERIFY_FAILED error", in this case do the following:
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
that way the error will be resolved.

Related

firebase_admin in kivy and buildozer

I'm using firebase_admin with python 3.8
On my computer it works very well but when I package the application with buildozer it breaks, does not run after installed on Android.
1 - Is there a mdelo application that shows the use with kivy?
2 - Is the spec file missing something?
Nothing is missing all you need is to add the following modules in your
buildozer.spec
requests, openssl, urllib3, chardet, idna, jwt, cryptography, pyparsing, firebase_admin, PIL, google-auth, cachetools, pyasn1, pyasn1_modules, rsa, google-api-python-client, google-cloud-storage, google-cloud-firestore, google-api-core, protobuf, cachecontrol, gcloud, google-cloud, httplib2
and in permission allow internet accesses like this
android.permissions = INTERNET
and you can write in your terminal for viewing logs while your app is running on your phone
buildozer android debug deploy run logcat
but you will need to allow to see only python logs by doing this in your buildozer.spec
[buildozer]
# (int) Log level (0 = error only, 1 = info, 2 = debug (with command output))
log_level = 2
if you didn't understand there is a Youtuber named Erick Sandberg you should watch his video about deploying an android app

APK made by buildozer not making CSV file on android mobile

I made an APK with the help of KivyApp. APK generated successfully and my APK also runs on my mobile. The problem is that I didn't give any specific path for the CSV file, which stored the output generated by the app. Initially, I run python code in the pydroid3 app and it automatically generated the CSV file at the same location, where my code was stored. My question is if I want to store the data in the internal storage of my mobile, what path should I enter?
import csv
csvfile = "Discrete_pos.csv"
with open(csvfile, "a") as fp:
wr = csv.writer(fp, dialect='excel')
wr.writerow(csvRow)
Well, I suppose you mean the path of External Storage (which can be accessed by any app, one you see in your mobile file explorer) As it's already storing in your internal storage path. Now to access external storage there are 2 methods. But first of all you need storage permission for that. To get storage permission use the following code:
from android.permissions import request_permissions, Permission
request_permissions([Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE])
Now after you have access to external storage you can use the following code to get external storage path:
from android.storage import primary_external_storage_path
external_storage = primary_external_storage_path()
Sometimes this doesn't work on some android devices so if you are getting that issue then you can also use os.getenv:
external_storage = os.getenv('EXTERNAL_STORAGE')
Also, don't forget to write READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE in the permissions of your buildozer.spec file

Android Kivy JsonStore data.json does not persist across upgrades

I'm using the JsonStore with a Kivy app
from kivy.storage.jsonstore import JsonStore
stored_data = JsonStore('data.json')
On my PC, the store is preserved even if I rebuild the program.
On my android phone, reloading the same apk file keeps the data, but any change to the program (and rebuild) seems to wipe the stored data.
Am I doing something wrong, how can I keep the data through upgrades?
That's because you write your files to the folder where your app is located. You should write them to /sdcard and also give the write permissions:
from jnius import autoclass
from android.permissions import request_permissions, Permission
...
# request a permission from user
request_permissions([Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE])
...
# path to sdcard (external storage that user have access to)
Environment = autoclass('android.os.Environment')
sdpath = Environment.getExternalStorageDirectory().getAbsolutePath()
So, after that you can make your folder there, something like:
if not os.path.exists(sdpath + '/yourappname'):
os.makedirs(sdpath + '/yourappname')
And save your files there, they won't be deleted while reinstalling the app anymore.
P.S. don't forget to add pyjnius to requirements in the buildozer spec file!

how to copy files from android to windows share using python?

Im trying to copy some files to and from a windows share. I am using python 2.7 and the pysmb library. This works fine with win7, but not with android. I build my connection over VPN with SMBConnection. listShares and listPath is working. Under win7 there is no problem using also os.path.isdir or shutil.copyfile. But that does not work with android. This is my code:
server_name = '192.168.168.12'
conn = SMBConnection(username=username, password=password, my_name='waldi2'
remote_name=server_name, domain=domain, use_ntlm_v2=True,
sign_options=SMBConnection.SIGN_WHEN_SUPPORTED, is_direct_tcp=True)
connected = conn.connect(server_name, 445)
# obtain a list of shares:
Response = conn.listShares(timeout=30) # !!!working, shows e.g.(' Share[', 20, '] =', u'JBW_in')
os.path.isdir('//192.168.168.12/JBW_in/') # is False with android, True with win7 !??
shutil.copyfile('/sdcard/waldi2_0/data/stop.txt', '//192.168.168.12/JBW_in/stop.txt')
This is no prob with win7, but with android it throws:
[Errno 2] No such file or directory: '//192.168.168.12/JBW_in/stop.txt'
Any clues? would be very very welcome ....
Or maybe alternative approaches to manage some python-based file-exchange between android and windows-server.
If I understund you correctly you are trying to connect to an android device via SMB. By my knowledge android doesnt have support for SMB server natively, but you could install a third-party app with a SMB server.

Android : change permission of system files

My phone is rooted and i wrote code in my app to change file permission of system file which is located at /sys/class/leds/lcd-backlight/brightness
Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("chmod 777 /sys/class/leds/lcd-backlight/brightness");
but this code can't change the file permission of the specified file and i won't got any kind of error or exception
Download the RootTools.jar from this link https://github.com/Stericson/RootTools/releases
Put that .jar file into your project's 'libs' directory
import com.stericson.RootTools.*;
.
.
Command cmd=new Command(0,"chmod 766 /sys/class/leds/lcd-backlight/brightness");
RootShell.getShell(true).add(cmd);
That's not how you execute su-ed commands. Please refer to http://su.chainfire.eu/ for guidance.

Categories

Resources