Android Question WriteList / ReadList with ExternalFile

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to All
I have a List of ExternalFile objects. The list is obtained on an external sd card, with:

B4X:
Dim FF as List
FF=Storage.ListFiles("Storage. ..." ) ' variables are correctly declared. This line is just to fix ideas

I want to save the objects in the quicker way. I had the idea to use TextWriter.WriteList. Therefore :
B4X:
Sub WriteFilesWrk(FF As List)
    Dim Ist As OutputStream
    Dim EE As ExternalFile
    Dim Txw As TextWriter
    
    EE=Storage.CreateNewFile(Storage.Root,NomeLav & ".wrk")
    
    Ist=Storage.OpenOutputStream(EE)
    Txw.Initialize(Ist)
    
    Txw.WriteList(FF)
    Txw.Close
    Ist.Close

End Sub

private Sub ReadFilesWrk
    Dim EE As ExternalFile
    
    EE=Storage.FindFile(Storage.Root,NomeLav  &".wrk")
    
    If EE.IsInitialized Then
        Dim Ist As InputStream
        Dim Txr As TextReader
        Dim LL As List
        
        Ist=Storage.OpenInputStream(EE)
        Txr.Initialize(Ist)
    
        LL=Txr.ReadList
        Txr.Close
        Ist.Close
    
    End If    
        
End Sub

At this point I get the attached file, which has 3 lines per each ExternalFile Object. Somewhere, in this forum, I have read that WriteList writes one line per Object. This seems not true, in my case.
The Notepad++ image shows what I see. The file is attached. Now, ReadList reads 3 lines. Must I reconstruct each ExternalFile with the 3 strings, or I must use another method? (Or am I missing something..) Thanks in advance.
 

Attachments

  • Immagine.png
    Immagine.png
    131 KB · Views: 58
  • Comporta2.zip
    90.4 KB · Views: 44

agraham

Expert
Licensed User
Longtime User
Why do you want to save the List? If you are thinking to save and restore it between invocations of your app to save doing ListFiles that probably won't work as the DocumentFile instances in the External Files will not exist and will need to be rebuilt when your app starts again.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
What are you doing??? Why write custom types as strings???
The new line character comes from the type conversion to string. Something that you should never use except for debugging.

Use B4XSerializator to convert the list to bytes.
Hi. Sorry Erel. I just tought that i could read/write a List of objects. What I understood was that this mechanism only excluded customized variables, but, for "customized" I mean something "user customized", not B4X specific objects. That's all. It is only a misunderstanding. I was already thinking to convert ExternalFiles to bytes and treat them, so thanks for confirming this strategy. Thanks again.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Why do you want to save the List? If you are thinking to save and restore it between invocations of your app to save doing ListFiles that probably won't work as the DocumentFile instances in the External Files will not exist and will need to be rebuilt when your app starts again.
Thanks for the bad new. Now my app, to get all the necessary files (JUST the references, not actually reading them), takes various minutes. It is mandatory for me to limit this drawback. Storing the ExternalFiles reference was my last beach.. Impossible to do it? Ok. Let's forget about usinng SD for my App.. Thanks a lot for avoiding me to waste more time..
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Does this app need to go in the Play store? With all that data to distribute it sounds not, in which case why not use
to let you store that data in an accessible location.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Does this app need to go in the Play store? With all that data to distribute it sounds not, in which case why not use
to let you store that data in an accessible location.
Andrew. I began this story on Sd cards with the very first problem: accessing it. The post is:


there, i cited your App ManageExternalStorage and Erel's ExternalStorage library App example. I wrote that your App was not "seeing" the Sd card and that only Erel's ExternalStorage library, with its Example App, was doing it. Conclusion was that only using ExternalStorage App i could do something. Did we return at the beginning? What I am missing? Thanks for your patience.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Anyway, for all readers, (if any), for me the topic is closed. Using Sd card is not the right choice for having a dynamic management of thousands of files. I did this, and actually the problem is not to read etc. As a matter of fact my App works well either in the GetSafeDefaultExternal folder or in the Sd card, with same thousands of files. The difference is that, while in GetSafeDefaultExternal you just supply the file loader with the file name, and this takes zero time, with the Sd you have to supply an ExternalFile object. (Thanks to AGraham I know that just giving the name to an ExternalFile previously obtained, is not the simply dirty solution). You "cannot" (in practice, in my case) get the ExternalFile object "on the fly", because it will take several seconds to do it, using ExternalStorage.FindFile. I mean: "you can", but your customer "cannot", because in this case, the App is dynamically displaying tens of bitmaps at every zooming/panning operation. So each "movement" on the screen implies waiting for tens of seconds. As an alternative, you can wait minutes at the beginning of the App, for having the list of all the (thousands) of files to use. Once done this, everythign works just like with GetSafeDefaultExternal. No practical difference. Finally, to restrict the drawback to wait minutes, to just a first time, I was thinking to permanently store the ExternalFiles references. Then, a second time, the App should only read these references. (The thousands of files will not change). But our friend AGraham said that this will not work, and of course I believe him. So, I don't see any way to go on.
The only open question is: Sd card price varies from 15 Euros to 50 or more. All of them declare more or less the same speed (if I saw well..). At this point I am glad to have bought the lowest price one, but maybe my greediness is the cause of my problems. Could a more expensive Sd card allow a ten times higher speed? Maybe this is an option. (Greedy man pays twice.. Russian proverb)
Forgive me if I wrote something wrong. I am not a guru at all. Gurus are free to correct me. I Just see the facts, in my situation. I wrote this post for anybody that could have a similar problem.. Thanks for reading.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Sorry, I didn't remember that other thread. Is there not enough memory in the primary storage of the device to hold all the files?

Yes, the speed of SD card access varies greatly according to the type of SD Card. The cheapest are bound to be very slow. You should try a class 10 card marked

that is preferably also marked as a class 3 card

See this Wikipedia article for more information than you really want to know
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Sorry, I didn't remember that other thread. Is there not enough memory in the primary storage of the device to hold all the files?

Yes, the speed of SD card access varies greatly according to the type of SD Card. The cheapest are bound to be very slow. You should try a class 10 card marked

that is preferably also marked as a class 3 card

See this Wikipedia article for more information than you really want to know
Hi. I have actually not such a big problem with device storage. It is mainly a problem of maintenance: first of all unluckily this tablet has an USb 2 interface. Copying the files to its memory takes hours, while copying on an Sd, on a computer, is much faster. Moreover, if I need to upgrade the thousands of files, which come from processing an OrthoPhoto which depends on a drone flight, which may be done various times over a zone where there are new constructions, I will need physically the Tablet. This latter may be far from me. Instead, I could just upgrade an Sd and send it. As you may argue, downloads and other ways of upgrading may be just theories. As a matter of fact, where new constructions, expecially in some zones ... Internet infrastructure may be very poor. But next Sd card will cost more, of course... Thanks again.
 
Upvote 0
Top