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
Related
I want to export a save file so the user can later import it into the app again later, but how do I allow exporting to a directory outside the app dir (/Android/com.app/files)
if (!(await Permission.storage.request().isGranted)) return;
final String? exportPath = await FilePicker.platform.getDirectoryPath();
if (exportPath == null) return;
File exportFile = await File(
"$exportPath/zs_tracker_data.sav",
).create(recursive: true);
This code is backed by these gradle properties:
android.useAndroidX=true
android.enableJetifier=true
With compileSdkVersion 33 selected, and these permissions:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
All of these above allows me to get a path, and then get this prompt:
However, even when I press 'Allow', I am not allowed to touch that directory and get: Unhandled Exception: FileSystemException: Cannot create file, path = '/storage/emulated/0/Backups/zs_tracker_data.sav' (OS Error: Operation not permitted, errno = 1)
I've considered allowing MANAGE_EXTERNAL_STORAGE, but then my app won't be allowed on Play Store, since the only reasons it needs storage permissions are for creating backups and importing backups. I've also seen this post: Flutter read/write to external storage with Android SDK 30+, but the answer blows over my head completely and seems horribly over complex for my simple backup export/importing...
How do I go about this in a decently simple way? I just want to be able to save the exported file somewhere where the user can easily find it
Maybe use a totally different approach which doesn't require any permissions. Use sharing to allow a user to choose whatever a destination she/he desires. For example using share_plus plugin:
Share.shareFiles(['<path to file>'], text: 'Export file');
I want to access Chrome browser directory in android/data and then copy files(TXT) From assets to that folder
1.how to get permission In Android 11
2.after getting permission How to copy any file from asset folder to that directory
Please give me an code that works like this
Image First
Image Second
App link above screenshot: https://play.google.com/store/apps/details?id=eu.tsoml.graphicssettings
If your app holds REQUEST_INSTALL_PACKAGES permission it can access Android/obb directory.
and for more details
Can and Android service running in background create directory in external storage public directory. If yes how to do it please share sample code.
However I am getting permission denied even after adding permit in manifest file
On Android 10 and above, have a look at the scope storage feature.
Source:- https://developer.android.com/training/data-storage
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 can I get path where my application is installed so I can access to raw folder and set some file as ringtone?
String path=android.os.Environment.getExternalStorageDirectory().getPath() + "sdcard/data/FunnySoundsz/res/raw/fusrodah.mp3";
But nothing happens with this. I set in my Manifest that app install only on Internal Storage