(android) read and load a txt file


#1

Is there a way to create a txt file at running time and read it on android using openfl?


#2

You can try sys.io.File to read and save, though Android is special in that files inside your application package aren’t real files at all, but are within a ZIP file. lime.utils.Bytes.fromFile should be sensitive to working within and outside the Android package, but Haxe standard APIs (like sys.FileSystem and sys.io.File) will only work with real files


#3

Hi I’m using sys.io.File but I get an error when I create the file and I have permission to write to the storage I don’t know what’s happening…
i am using this examples to do my own https://code.haxe.org/category/beginner/using-filesystem.html


#4

What path are you using? Does the directory exist?


#5

i am trying to create a directory also but i prove without the directory an have the same problem also use create the text directly and also use the Sys.programPath to make my path to go but get the same problem or there’s a way to get this path from another way?


#6

Have you tried using one of the lime.system.System paths, like System.applicationStorageDirectory?


#7

it crash… :frowning: i’m trying to do a save system form an older version of openfl to the newest version because this save disappear i don’t find anything that help me with this issue.


#8

Does this help? Need help loading old android and ios saves


#9

Hi, i can do it with the File.saveContent() and getContent() in the older version of haxe but with the newest only crash the application what could be wrong… I am using also FileSystem to know if the file exist and deleting the file after i used in my code:

#if !(flash || html5)
import sys.FileSystem;
import sys.io.File;


#end

 public static function saveInFile():Void
 {
	#if !(flash || html5)

	var root = Xml.createElement('data');
	var fileName:String = "/mnt/sdcard/data.zip";

  child = Xml.createElement('APIs');
  child.set("challenges",""+myChallenges);
  child.set("user",""+userLevel);

  root.addChild(child);
  var content:String = root.toString();

  var encoded = haxe.crypto.Base64.encode(haxe.io.Bytes.ofString(content));

  File.saveContent(fileName,encoded);

 }

public static function loadFromFile():Xml
{
	var fileName:String = "/mnt/sdcard/data.zip";
	var exist:Bool = false;
	var root:Xml=null;
	#if !(flash || html5)
	exist = FileSystem.exists(fileName);
	var content:String="";
	if (exist) 
	{

        content = File.getContent(fileName);
		
		var decoded = haxe.crypto.Base64.decode(content).toString();
		root = Xml.parse(decoded);
		myChallenges = Std.parseFloat(game.get("challenges"));
		userLevel= Std.parseInt(game.get("user"));
		FileSystem.deleteFile(fileName);
    }

#10

Are you sure it crashes on File.getContent, and not on Base64 decoding, or some other line of code here?


#11

i am going to try it and i let you know


#12

the game crashed at this line

content = File.getContent(fileName);

i made a separated test only with the getContent of another file hello.txt to discard the text encripted an it crashed


#13

Does it work if you use File.getBytes instead?


#14

the same crash but i made a try and catch and throw me this error
Error: [file_contents,/mnt/sdcard/hello.txt]


#15

Hmm, usually that occurs when you have no permissions to read a file, or the file doesn’t exist


#16

I alread have write and read permissions and i verify that the project and also the AndroidManifest.xml have them. What could be missing?


#17

What changed since this worked before? Is it a different Android OS version, a different device, a different Haxe release…?


#18

i have two pc with haxe:

pc with the olders versions of haxelib:

`haxe 2.3.1

actuate: 1.7.5 1.8.0 1.8.1 1.8.2 1.8.3 [1.8.6]

extension-admob: 1.4.5 [1.4.6]
extension-facebook: [0.1.0]
extension-googleplaygames: 1.3.2 [1.3.3]
extension-googleplayservices-lib: [1.1.0]
extension-locale: 1.0.0 [1.0.1]
extension-share: 1.2.2 2.1.0 [2.2.1]
flambe: 4.0.0 [4.1.0]
flixel-addons: [1.1.0]
flixel-demos: 1.1.1 [1.1.2]
flixel-templates: [1.0.2]
flixel-tools: 1.0.2 1.0.4 1.1.0 [1.1.1]
flixel-ui: [1.0.2]
flixel: [3.3.11] 3.3.4 3.3.6
format: 3.0.5 3.1.2 3.2.0 [3.2.1]
ganalytics: [0.0.3]
haxe-ga: 0.4.20 0.5.1 [0.5.2]
HaxePunk-nape: [1.0.1]
HaxePunk: 2.5.2 2.5.3 [2.5.6]
haxeui: 1.6.15 1.7.0 1.7.14 1.7.16 [1.7.18]
howlerjs: 1.1.25 [2.0.2]
hscript: 2.0.4 [2.0.5]
hxcpp: 3.1.30 3.1.39 3.1.48 3.1.68 3.2.102 [3.2.180]
hxlibc: [1.1.4]
hxsl: 2.0.3 2.0.5 [2.0.6]
inthebox-macros: [1.2.0]
lime-tools: 1.3.0 [1.5.7]
lime: 2.5.2 2.5.3 2.6.0 2.6.8 2.6.9 [2.7.0]
nape: 2.0.16 [2.0.19]
openfl-adbuddiz: 0.1.1 0.1.2 [0.1.4]
openfl-admob: 1.2.3 1.3.2 1.3.3 1.3.4 1.4.1 [1.4.2]
openfl-bitfive: 1.0.5 2.0.0 2.1.6 3.0.0 [3.3.3]
openfl-gpg: 1.1.0 1.3.0 [1.4.3]
openfl-gps-lib: 1.0.2 1.0.4 [1.4.3]
openfl-html5-dom: 1.2.1 [1.2.2]
openfl-html5: [1.0.5] 1.4.2-beta
openfl-locale: 1.0.0 [1.0.1]
openfl-native: 1.2.4 [1.4.0]
openfl-ouya: [1.0.2]
openfl-samples: 1.3.0 2.2.1 2.2.2 3.1.0 [3.3.1]
openfl-share: 1.2.0 [1.2.2]
openfl-tapjoy: [1.0.0]
openfl-tools: [1.0.10]
openfl-unityads: [1.0.3] 1.0.5
openfl: 2.2.8 3.2.2 3.3.8 3.3.9 [3.4.0]
OpenFLMopub: [1.0.0]
phaser: 1.0.0 1.1.0 [1.1.1]
samcodes-admob: 1.0.0 1.0.1 1.0.3 1.0.4 [1.0.5]
samcodes-chartboost: 1.0.0 [1.0.1] samcodes-chartboost-master.zip
spritesheet: [1.2.0]
svg: [1.0.8]
swf: 1.5.2 1.7.6 1.7.7 1.8.9 1.9.1 2.1.2 [2.1.3]
systools: [1.1.0]
yagp: [1.1.4]

And the other computer have this versions:

haxe 3.4.7
actuate: [1.8.7]
advanced-layout: [0.7.0]
box2d: [1.2.3]
createjs: [1.5.8]
Enhance-Extension: [1.0.0]
extension-android-support-v4: [1.0.0]
extension-googleplaygames: [1.5.7]
extension-googleplayservices-basement: [1.1.0]
extension-googleplayservices-lib: 1.1.0 [1.1.1]
extension-iap: [1.2.2]
extension-locale: 1.0.1 [1.0.3]
extension-share: 2.4.2 [2.4.3]
flixel: 4.4.1 [4.4.2]
google-play-services: [1.1.0]
haxe-ga: 0.5.2 0.5.3 [0.5.4]
HaxePunk-nape: [1.0.1]
HaxePunk: 2.5.3 2.5.6 2.6.1 4.0.0 4.0.1 [git]
hxcpp: 3.2.180 3.3.49 3.4.64 3.4.188 [git]
layout: [1.2.1]
lime-samples: 2.6.0 4.0.1 [6.2.0]
lime-tools: [1.5.7]
lime: 2.1.3 2.5.2 5.4.0 5.9.1 6.0.1 6.1.0 6.2.0 6.3.1 [6.4.0]
mapsheet: [1.0.0]
mconsole: [1.6.0]
mcover: [2.1.1]
mlib: [2.0.2]
mobileprefs: [1.0.0]
munit: 2.1.2 2.2.1 2.2.2 [2.3.0]
nape: [2.0.20]
nme: [6.0.58]
openfl-ouya: [1.0.2]
openfl-samples: [6.0.0]
openfl-share: [1.2.2]
openfl-unityads: 1.0.3 [1.0.7]
openfl: 2.2.8 3.2.2 6.1.0 6.5.3 7.0.0 7.1.0 7.1.1 7.1.2 8.0.2 8.1.0 8.1.1 8.2.2 [8.3.0]
Pathfinder: [0.2.18]
samcodes-admob: 1.0.9 [git] master samcodes-admob-master.zip
samcodes-chartboost: 1.0.1 1.0.4 [1.0.9]
samcodes-notifications: [1.0.5]
spritesheet: [1.2.0]
unityads_dome: [0.0.1]

The android device is the same for the two apks in the older version run without a problem


#19

I wonder if this is somehow an issue with the HXCPP version you have, File.getContent ultimately boils down to a C++ extern call into HXCPP methods


#21

https://drive.google.com/file/d/1FUPZlOdWDVkVXShWx_ivIso-Szpanp38/view?usp=sharing

I created an empty openfl project to test the “Load and Save File” features…
This doesn’t work on Android, can you confirm if this works for you @singmajesty ?