pass data from one app to another

mattdavis523

Member
Licensed User
Longtime User
I have used random access file to save data for my app, stats that I want to load later. I have two versions of my app, one free one paid, but the coding is the same. when I am testing the apps on xoom i get an error if the free version tries to open a file that was created by the paid version. I get a classcastexception error that says the data can't be cast to the app. is there some way to load the file with a different app?
 

mattdavis523

Member
Licensed User
Longtime User
The files are located on dirrootexternal in a directory that I created on the sd card. Both apps save and load files to/ from the same location.
 
Upvote 0

mattdavis523

Member
Licensed User
Longtime User
here is the code which is the same for both apps. it works just fine for one app, until the other tries to read an object the opposit wrote.

I've also attached a screen shot of the error i get

thanks for your help

B4X:
Sub Build_Stats_dat
   Dim raf As RandomAccessFile
   If File.Exists(File.Combine(File.DirRootExternal,"Volleyball/data"), "Last_Game") = False Then
   File.MakeDir(File.DirRootExternal,"Volleyball/data")
   End If
   raf.initialize(File.Combine(File.DirRootExternal,"Volleyball/data"), "Last_Game", False)
   raf.writeobject(players, False, 0)
   raf.writeobject(GameScores.Text, False, raf.CurrentPosition)
   raf.writeobject(Opponent.Text, False, raf.CurrentPosition)
   raf.WriteObject(RosterEt.Text, False, raf.CurrentPosition)
   raf.Close
End Sub
Sub Load_Stats
   If File.Exists(File.Combine(File.DirRootExternal,"Volleyball/data"), "Last_Game") = True Then
   Dim raf As RandomAccessFile
   raf.initialize(File.Combine(File.DirRootExternal,"Volleyball/data"), "Last_Game", False)
   players = raf.readobject(0)
   GameScores.Text = raf.ReadObject(raf.CurrentPosition)
   Opponent.Text = raf.ReadObject(raf.CurrentPosition)  
   RosterEt.Text = raf.ReadObject(raf.CurrentPosition)
   raf.close
   CreateListOfPlayers(players.Size, False)
   End If
   If players.Size = 0 Then Msgbox("Press Menu then Edit Players or Load Roster","To Get Started")
End Sub
 

Attachments

  • Screenshot_2012-04-04-17-37-26[1].jpg
    Screenshot_2012-04-04-17-37-26[1].jpg
    57.8 KB · Views: 178
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
@matt

You're using Random Access Files but writing textual data to the file. I'm not sure this is ok. Maybe someone more experienced can jump in and say for sure. I may be barking up the wrong tree.

You should be writing to Long data types (you can use other data types but need to cast them to Long).

According to the docs here you should be reading and writing non-textual data types.

I'm reading and writing numeric values too and using the TextReader file type and it works just fine. Much easier than using RAF. PM me and I will send you my Subs to read and write the numeric data if you want.
 
Upvote 0

mattdavis523

Member
Licensed User
Longtime User
Players is a list comprised of players which is a user defined variable type, not text that's why I was using random access file. But if I store the text as long then I could still use this setup???
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
The code you posted was missing the commented items:

B4X:
Sub Build_Stats_dat
   Dim raf As RandomAccessFile
   If File.Exists(File.Combine(File.DirRootExternal,"Volleyball/data"), "Last_Game") = False Then
      File.MakeDir(File.DirRootExternal,"Volleyball/data")
   End If
   raf.initialize(File.Combine(File.DirRootExternal,"Volleyball/data"), "Last_Game", False)
   raf.writeobject(players, False, 0)
   raf.writeobject(GameScores.Text, False, raf.CurrentPosition)
   raf.writeobject(Opponent.Text, False, raf.CurrentPosition)
   raf.WriteObject(RosterEt.Text, False, raf.CurrentPosition)
   raf.Close
End Sub
Sub Load_Stats
   If File.Exists(File.Combine(File.DirRootExternal,"Volleyball/data"), "Last_Game") = True Then
      Dim raf As RandomAccessFile
      raf.initialize(File.Combine(File.DirRootExternal,"Volleyball/data"), "Last_Game", False)
      players = raf.readobject(0)
      GameScores.Text = raf.ReadObject(raf.CurrentPosition)
      Opponent.Text = raf.ReadObject(raf.CurrentPosition)
      RosterEt.Text = raf.ReadObject(raf.CurrentPosition)
      raf.close
      CreateListOfPlayers(players.Size, False)
   End If
   If players.Size = 0 Then
      Msgbox("Press Menu then Edit Players or Load Roster","To Get Started")
   End If 'This was missing   
End Sub
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
I think you could do that yes. Casting to a Long should work from what I can tell from the docs.

Here is my read and write subs using the TextReader file I/O methods and notice I'm storing Int data types. You can add string types and booleans (as 0 or 1). I do this in other data files I use. It handles all three data types very well and is easier to use than RAF I/O type, in my opinion.

B4X:
Sub Read_From_Dat_File
   Dim mFilePath As String
   If File.ExternalWritable = True Then
      'SD Card Installed
      mFilePath = File.DirRootExternal
   Else
      'No SD Card
      mFilePath = File.DirInternal
   End If
   If File.Exists(mFilePath, pDisplay_dat_filename) = True Then
      Dim tr As TextReader
      tr.Initialize(File.OpenInput(mFilePath, pfilename))
      pDiskAvailInt1 = tr.ReadLine
      pDiskAvailInt2 = tr.ReadLine
      pDiskAvailInt3 = tr.ReadLine
      pDiskAvailInt4 = tr.ReadLine
      tr.Close
   End If
End Sub


Sub Write_To_Dat_File
   Dim mFilePath As String
   If File.ExternalWritable = True Then
      'SD Card Installed
      mFilePath = File.DirRootExternal
   Else
      'No SD Card
      mFilePath = File.DirInternal
   End If
   Dim tw As TextWriter
   tw.Initialize(File.OpenOutput(mFilePath, pfilename, False))
   tw.WriteLine(pDiskAvailInt1)
   tw.WriteLine(pDiskAvailInt2)
   tw.WriteLine(pDiskAvailInt3)
   tw.WriteLine(pDiskAvailInt4)
   tw.Close
End Sub
 
Upvote 0
Top