AI2 - File component - Error 2104: Cannot write to file

905 views
Skip to first unread message

sn....@gmail.com

unread,
Sep 11, 2014, 8:32:16 AM9/11/14
to mitappinv...@googlegroups.com
Hi,

thanks to you guys my program gets bigger and bigger :-)

I am trying to write a report with "Call File1.AppendToFile"

I start with no file.

I try to write a new file which I named /output1.txt (so I I will find it at /storage/sdcard0/output1.txt).

I only use the "Call File1.AppendToFile" to write lines to that file.

I have gathered my data, but as soon as I start to write, I run into  Error 2104: Cannot write to file /storage/sdcard0/output1.txt.

I end up with an empty file named: /storage/sdcard0/output1.txt.

Is there an easy answer :-)

TIA


SteveJG

unread,
Sep 11, 2014, 9:09:12 AM9/11/14
to mitappinv...@googlegroups.com
You are trying to append to a file that does not exist.  The first time you create and write to the File, you should be using  File1.Save   text    fileName.

Also you should be Appending to  /storage/sdcard/0/output1.txt    and you actually wrote to  /storage/sdcard0/output1.txt.    or the reverse?

You said you try to recover from  /storage/sdcard0/output1.txt  ?    There is no such directory, the directory is  /storage/sdcard/0/output1.txt  probably 



If you have a File Manager app on your device, you should be able to find the correct file location of output1.txt.     Can you find it?

Could you show an image of your blocks, showing what you are trying to do?


Regards,
Steve

sn....@gmail.com

unread,
Sep 11, 2014, 10:37:41 AM9/11/14
to mitappinv...@googlegroups.com

Hi Steve :-)

the mistakes in the filenames were probably typos, because I use a variable.

I have made a new (this time small) program, which generates the same mistake :-)

TIA


SteveJG

unread,
Sep 11, 2014, 10:53:06 AM9/11/14
to mitappinv...@googlegroups.com
With this code, if you run the program and save the file, it will save.    If you close the app, then re run it, the app thinks that it is still FirstWrite = J   because you do not save the fact that the first write for the file \Bestand.txt is NOW  nein.

When you rerun the code, Screen1 reads the block  set global FirstRight to J   and when it gets to the WriteButton.Click   it sees the "J" and decides to overwrite your file with first run ... eerste regel.

You need to establish a way to save the FirstWrite flag, whether it is part of the actual file you save or in a TinyDB. 



--Steve


Taifun

unread,
Sep 11, 2014, 11:18:39 AM9/11/14
to
this example is a little bit different, but probably it helps
Taifun

sn....@gmail.com

unread,
Sep 11, 2014, 12:02:56 PM9/11/14
to mitappinv...@googlegroups.com
Hi Steve,

The FirstWrite variable was only in this program to be absolutely sure there WAS a file. In real life I wouldn't do it this way.

I don't understand what I must do to avoid the error message????

It sounds as if you mean:
  • Call File1.SaveFile
  • Call File1.AppendToFile
    ....
  • Call File1.SaveFile

(Because you say: With this code, if you run the program and save the file, it will save.

Do I some how have to close the file?

(Ain't there an example in the docs?)

Last question:

If you open file A and write to file B, do you need 2 file components on the design screen? (that is File1 and File2)

TIA




SteveJG

unread,
Sep 11, 2014, 12:52:37 PM9/11/14
to mitappinv...@googlegroups.com
I can answer your last question right now.   You only need one component (unless you change screens).

The other question, I need to think about a bit and am busy right now.   perhaps someone will answer that before I get back from some errands.

Taifun

unread,
Sep 11, 2014, 1:08:17 PM9/11/14
to mitappinv...@googlegroups.com
after reading the file the first time, you have to store that you read it in TinyDB 
currently you set FirstWrite to J on each run of the app
see this similar example how to do it
Taifun

sn....@gmail.com

unread,
Sep 11, 2014, 4:29:24 PM9/11/14
to mitappinv...@googlegroups.com
Hi Taifun :-)

That reading the file the first time is only in this example.
I don't need it and I would do it another way anyhow.
If I would use a file for writing in multiple sessions, I would first check if the file existed (has content). No need for TinyDB.

In this example, I would only check in this session if I can Append to the file because it exists :-)

I must make my example even smaller to test the CALL's and see when it writes the text to the file.

I am very surprised that you and Steve respond so very fast to our questions/problems.
We are lucky that you two (probably other people too) help us and I can't thank you two enough for that :-)

I don't know what I must do to solve my problem, because you two seem to think that it must work this way, and I still get my Error :-(

But for now,

Thanks!

sn....@gmail.com

unread,
Sep 11, 2014, 4:51:58 PM9/11/14
to mitappinv...@googlegroups.com


Hi

why is this giving me Error 2104: Cannot write to file /storage/sdcard0/FileA.txt ?

TIA :-)


SteveJG

unread,
Sep 11, 2014, 5:25:10 PM9/11/14
to mitappinv...@googlegroups.com
Hi TIA,  

I do not know why you get an error message.  I coded what you show. The code runs OK on both my tablet and on the emulator.




Are you running this on an Android device (WIFI or USB) or an emulator.

I do not see the error message.

-- Steve

Taifun

unread,
Sep 11, 2014, 5:47:19 PM9/11/14
to mitappinv...@googlegroups.com
the screenshot looks fine, it "should" work...
did you try my example? did it work for you?
Taifun

sn....@gmail.com

unread,
Sep 11, 2014, 8:12:33 PM9/11/14
to mitappinv...@googlegroups.com
?????

Remarkable!

Samsung GT-i8160 ---> USB ---> iMac          +         MIT AI2 Companion

I will build this again in a new project.

If it works for you, it will work for everyone. Then the problem must be on my side.

It is bedtime now

But, I'll be back

Thanks

sn....@gmail.com

unread,
Sep 11, 2014, 8:14:04 PM9/11/14
to mitappinv...@googlegroups.com
I haven't tried your example yet.
But I will try it tomorrow.

Thanks :-)

sn....@gmail.com

unread,
Sep 12, 2014, 6:48:38 AM9/12/14
to mitappinv...@googlegroups.com
I have built the blocks again and still the same mistake.
My guess is: it has something to do with memory.

Further proof. A game I am playing on this device lacks things due to the fact that it can't install his upgrade :-(

I say: put this on hold. I will further investigate it.

SteveJG

unread,
Sep 12, 2014, 7:25:41 AM9/12/14
to mitappinv...@googlegroups.com
OK.   Is it possible your device does not have a memory card installed?  Or, the memory is almost filled because of lots of files and apps?  The code tells the File control to write to the card.
You are using USB to connect, can you run your code on the device using WIFI?    If so, try it.   Can you use the emulator?   Try it.
Why?  The issue could be using the USB connection.  The issue might be there due to how the usb drivers on your PC use the data, but that is a guess.

Another way to try it is not to write to the card... do not use the / and have it write to the app internally.  Not as flexible when you use the File control that way but it should work, even if your device does not have an sdCard.

Good luck.    Yes, your code works fine on my devices.

-- Steve

sn....@gmail.com

unread,
Sep 12, 2014, 8:19:06 AM9/12/14
to mitappinv...@googlegroups.com
I have built and installed the app on my device and still the same error.
(At least I could install it :-)

As long as you haven't heard other people having the same problem, I guess you don't need more precise info on Android version (4.1.2) or free memory (Total 1,2Gb, free 296 Mb).

NB
I have tried rebooting the device.

sn....@gmail.com

unread,
Sep 12, 2014, 8:25:51 AM9/12/14
to mitappinv...@googlegroups.com
@Taifun

I've imported your .aia file, but it resulted in the same mistake :-(

Thanks

NB
TIA = Thanks in advance (not my name, although my parents could have given me that name :-D )

sn....@gmail.com

unread,
Sep 12, 2014, 11:19:48 AM9/12/14
to mitappinv...@googlegroups.com
Hi guys,

everything works like it is supposed to do :-)

I have freed diskspace and RAM on my device and it's going great again :-D

THANKS
Reply all
Reply to author
Forward
0 new messages