Math

Save results to restore

Hello,

I am using android math Widget and I'd like to save the content of widget. I am trying to save bytes array of serialize() method to file but when I loaded the widget is empty. Can I save the content of widget to any file (except image,latex etc) to use it again when I load the widget?

Thank you,
Kyriakos


Dear Kyriakos,

To our knowledge, we have not been reported with such issues at present.

In a first time, you should save the context proceeding as follows:
byte[] mWidgetSerialized = mWidget.serialize();

It can be restored later proceeding this way: mWidget.unserialize(mWidgetSerialized, true);

Is this the way you proceed?

Best regards,

Olivier

Hello Olivier,

Thanks for your answer.

Yes, I save the byte array of mWidget and I know that needed to unserialize this bytes to have access.

That I want to do is to save this bytes for second time even the app has been closed.

Best regards,
Kyriakos

Dear Kyriakos,

normally, if storing the byte array in a file, you should be able to re-open and unserialize it.

Best regards,

Olivier

Hi Olivier,

Thanks for your answer but everything that I tried to store the bytes array of MAW widget failed.

I am trying to save the byte array (result of serialize() method of widget) using the following method:

 public static void saving(Context context, String path, byte[] array) {

  try {
   FileOutputStream out = new FileOutputStream(path);
   out.write(array);
   out.close();
  } catch (Exception e) {
   e.printStackTrace();

  }
 }

And for retrieve the byte array I am using the following method:


public static byte[] loading(Context context, String path) {

  byte[] mybytes = null;

  try {
   File file = new File(path);
   FileInputStream FIS = new FileInputStream(file);

   mybytes = new byte[(int) file.length()];

   FIS.read(mybytes);
   FIS.close();

   return mybytes;

  } catch (Exception e) {
   e.printStackTrace();

  }
  return null;
 }


I am using the above method results to unserialize the widget. (mWidget.unserialize(array, false);).

I get no exception and no results to my screen.

Am I missing something that is important for serialization or unserialization? I am looking for the answer few days but I can't find the reason for this.

Best regards,
Kyriakos

Could you please try to use the following code to save the result:

private void save(String fileName, byte[] array) {
  String path = Environment.getExternalStorageDirectory() + "/" + fileName + ".test";
  File file = new File(path);

  if (file.exists()) file.delete();

  // Write the byte array to the specified path
  try {
   OutputStream bos = new BufferedOutputStream(new FileOutputStream(path));
   bos.write(array, 0, array.length);
   bos.flush();
   bos.close();

  } catch (Exception e) {
   e.printStackTrace();
  }

 }

Do you get the file under the sdcard folder? It requires to add those permissions

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


To restore the file, please use the following method:

private byte[] restore(String filename) {
  byte[] data = null;
  String path = Environment.getExternalStorageDirectory() + "/" + filename + ".test";
  File file = new File(path);
  data = new byte[(int) file.length()];
  try {
   new FileInputStream(file).read(data);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return data;
 }
}


Call the save method:

public void onRecognitionEnd() {
  if (DBG)
  Log.d(TAG, "Equation recognition end");
  serializeResult = mWidget.serialize();
  save(filename, serializeResult);
 }

Call the restore method:

public void onClick(final View view) {
  serializeResult = restore(filename);
  SystemClock.sleep(200);
  if (mWidget.unserialize(serializeResult, true) == false) {
  Log.d(TAG, "Unserialization failed");
}
 }

Best regards

Login or Signup to post a comment