KIvy Android how to create and read a file - android

After looking all over google I haven't found a way of accessing Android internal storage from python.
I need to store an image generated by kivy app. I would like my python code to be able to navigate to a root user dir, create an app specific dir (if not found) and save the image there.
Simply creating a file with my device's path doesn't work. Should I be setting permissions for internal storage access? I'm lost with it.

You have something wrong in your code, you didn't paste the code nor logs, so... let's get it another way.
You can read/write with python just fine in the app folder (/data/data/org.something) with using:
app_folder = os.path.dirname(os.path.abspath(__file__))
To write to SD card you'll need to use App.user_data_dir, which as you can see in the docs on android gets you /sdcard/<app_name>. Not sure if it automatically creates it if it's missing, so it needs checking out probably. However, if it doesn't exist, just create it with python:
os.mkdir(App.user_data_dir)

Related

Read/write from Android/data folder with Flutter

I recently decided to experiment a bit with flutter and Android development, and I'm trying to create a simple app to import and export a game saves (because you can use the file both on Android and PC, but there isn't an official sync system yet), but I'm having troubles with file management. Basically, what I would like to achieve is the following:
Export: the app should copy a file from the Android/data folder of the game to another location (example: download folder)
Import: the app should let you pick a file and then copy it to the data folder of the game
Right now I managed to ask for both storage and manageExternalStorage permissions when launching the app, but even with those I get an access denied error (errno = 3) when trying to export the file.
Is it even possible to achieve my goal? Or maybe I'm just doing something wrong with permissions?
I'll give more info about the app (for example code snippets) if you have specific requests on some aspects of it

Android: Storage Access Framework - Just need to copy a file to extSD

Existing Xamarin Forms app on Android.
New feature request from the bosses: To copy some updated documents from a USB FlashDrive to a known directory on the extSD already in the tablet.
Seems like that shouldn't require a PhD. Everything we've done to date has been on the internal storage so typical System.IO calls work fine. But Android has this whole Storage Access Framework stuff in place for things like the external SD, or Google Drive or where ever.
Every question and tutorial I see are all for the same thing: How to open a document browser to the SD card, or how to create a new CREATE DOCUMENT INTENT.
I'm not looking to do any of that. Don't need a UI. Not making a new text file from scratch.
I just want to copy a file from the flashdrive to the extSD. Everything else is in place to do this to internal storage. Easy-Peezy.
Its a single line when copying from the flashdrive to the internal SD.
FileInfo.CopyTo(targetAddress, true);
How do I copy a file to the extSD? Do I really need 200 lines of overhead making a DOCUMENTPROVIDER, and CONTRACT and 20 other things just to copy a file? All I get is "ACCESS TO extSD 1234-5678 is denied". Seems like it should be fairly simple to get permission to the card then just copy a file or make a new folder. But I swear everything I read for SAF makes it sound like you have to make 10 classes and a manager for them all first.
Anyone got a simple example of the minimalist way to get write permission to the extSD and copy from A to B?
If it is copying only one file them its pretty simple. Let the user select the fle with ACTION_OPEN_DOCUMENT. You get an uri for the file. Now let the user create a file at the right place using ACTION_CREATE_DOCUMENT. You got another uri. Now open an InputStream and an OutputStream for those uries. Then read bytes from input and write to output. Less then 200 lines ;-)

How to create file on android using xamarin?

I've tried several folders but looks like no file is created.
External read & write permission have been enabled.
The code run without any sign of error. (as given below)
Here is the screenshot of the code inside mainactivity.cs (this is for testing purposes).
I also assume that since this is platform specific, the code must be in android project.
string filename = Path.Combine(Xamarin.Essentials.FileSystem.AppDataDirectory, "count1.txt") ;
StreamWriter sw = File.CreateText(filename);
sw.WriteLine("asad ini test");
sw.Close();enter code here
1b. I 'm using Android 6.0 phone, but I can't find such folder /data/user...
(I'm not sure if such folder is targeted special version of Android)
I search both internal /Android/myproject/files/ and found nothing
I search external SDcard /Android/myproject/files/ and still found nothing
(please look at the my android phone folders, i can't even find /data folder
Once, this is accomplished the next step would be how to call this function from the generalproject (non android, non IOS, non UWP project).
Example to write file given in xamarin document.
Where should this code reside? in general project folder ? or, android project folder?
The term "external" used in android is very misleading. It doesn't mean the external removable SD card rather some folders that don't require "root" permission. Therefore, initially I can't find the folder which I save a file into.
Another reason is each phone manufacture uses different name, hence it is very difficult to get an absolute address of an external removable SD Card.
More details is explained in blog_external_removable_SD_Card
You can't write to the removable SD Card
an old post here

When using emulators, how to interpret the output path when calling Context.getFilesDir()?

Inside onCreate() I have this line:
File aux = context.getFilesDir();
which outputs this:
/data/user/0/com.example.tirengarfio.myapplication/files
but.. where is this path exactly insde Linux filesystem? or is it taking as reference the Android Studio Project root directory? os should I create it somewhere?
EDIT:
As I said to #Simas, but by the moment Im not connecting any smartphone. Im just using emulators on Linux. My intention is just reading a file using
FileOutputStream fos = openFileInput(FILENAME, Context.MODE_PRIVATE);.
So, where should I place the file inside the Linux filesystem?
This is the path of your app's local data folder. It can either be in the memory card or the device storage.
There's no easy way to access it if your device is not rooted but here's a starter:How to access data/data folder in Android device?
Let us see what the doc says :
Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.
No permissions are required to read or write to the returned path, since this path is internal storage.
So actually the files you will find are those which were saved with the very same function openFileOutput(String,int) by your/other applications.
So basically if you want to test some functionality (which I suppose) write a unit test that uses this API openFileOutput(String,int) to store some mockup data and then get it again with Context.getFilesDir() and some code for file processing.

Creating a .pcap file in native code in Android then retrieving it

I have an app that uses native code to generate a .pcap file (amongst other things). The idea is that while running the program, the pcap file is created and I can later retrieve it by say plugging in the phone into my laptop and then access it.
I use fopen(), fwrite() and fclose() in my native code to create the .pcap file. My question is, how should I specify the filename and filepath? For example, when I run it on windows, I just use "test.pcap" and it generates it in the same folder, but how does this work on android? As I mentioned before, the file is created down in native level.
Native or not, the directory structure should be the same.
You can create a directory in /flash called pcap. But check it exists first, as it can be /nand. Use /sdcard if you want to write in the SD card.
After that you can use, say, /flash/pcap/test.pcap as path for the file. The file would be created in the internal flash memory.

Categories

Resources