0

I'd like to store an array of floats (lets say it will be around 5000 floats) to my sdcard. Later I'd like to put my file of float on my resources app so a new app can use those floats for later processing. Can I do this?? Is there any example I could use to do so? Which file extension would be best to use?

Thanks!

strv7
  • 3
  • 2

2 Answers2

3

I'm not very familiar with Android, but from a Java perspective, does it make sense for you to use serialization and ObjectOuputStream / ObjectInputStream? It sounds like this would do exactly what you want. Some related discussions can be found here:

How to Serialize a list in java?

http://www.jguru.com/faq/view.jsp?EID=34789

Community
  • 1
  • 1
Carl
  • 905
  • 5
  • 9
0

First, you will need to add the appropriate permission to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

You will also want to check (at runtime) if the device running your application has available external storage:

String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state))
{

  // Read / Write Access
  externalStorageAvailable = true;
  externalStorageWriteable = true;

}
else if(Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
{

  // Read Access
  externalStorageAvailable = true;
  externalStorageWriteable = false;

}
else
{

  // We cannot read / write
  externalStorageAvailable = false;
  externalStorageWriteable = false;

}

Finally, you can write the file by doing something like the following...

FileOutputStream fileStream = null;
PrintStream printStream = null;
File file = new File(FILE_NAME);

try
{

  fileStream = new FileOutputStream(file);
  printStream = new PrintStream(fileStream);

  for (int i = 0; i < FLOAT_ARRAY_SIZE; i++)
  {

    printStream.print(FLOAT_DATA[i]);
    printStream.println();

  }

}
catch(Exception e)
{

  e.printStackTrace();

}
finally
{

  if(printStream != null)
  {

    printStream.close();

  }

}

As stated before, a '.bin' or '.dat' extension is fine.