Suppose there is a class Foo.java, which I serialized in a desktop Java application in order to use it in an Android application. The result was the testSerial.gbw file, which I put in the project directory of Android-Studio next to the packages of my Android application (see screenshot).

Screen from android studio

I also added the Foo.java class template to the Android application project in order to perform classical (as I thought / wanted) deserialization:

FileInputStream fis = new FileInputStream(path); ObjectInputStream ois = new ObjectInputStream(fis); Foo foo = (Foo) ois.readObject(); 

As a result, I did not even think about the fact that this is an android and not java, and after compiling the application is already running on the device in which a) another file system b) the application is packaged in the app. From b) the question also follows, where does the testSerial.gbw file go after the project is packaged in the app? How can I find this file and connect it in my application? Roughly speaking, how and where to find my testSerial.gbw in an already compiled application, or where to put it? Or did I initially choose the wrong way to serialize?

    2 answers 2

    Deserialization will most likely work fine. But the files to which you want to access in runtime on the android should be placed in the assets folder (if the memory does not change at the same level as the java folder). You can read the file put there as follows:

     context.getResources().getAssets().open("testSerial.gbw") 
    • yes it works. Already myself, thank you! - torin.dmitry
    • @player, if the answer is correct, mark it as correct - Vladyslav Matviienko

    In Android Studio, create the Assets directory:

    File -> New -> Folder -> Assets Folder

    Put in this directory need a file for deserialization, it turns out:

    assets / wastedtime.ser

    Now we can use AssetManager to access the file:

     AssetManager am = getAssets(); InputStream fis = am.open("wastedtime.ser"); //возвращает InputStream а не FileInputStream ObjectInputStream ois = new ObjectInputStream(fis); wastedtime = (MyClass) ois.readObject(); ois.close(); fis.close(); label.setText(wastedtime.getWastedTime()); 

    Everything works, now of course the problem with the versions of the classes, an error falls out

    enter image description here

    But this, as they say, is another story.

    • The problem of compatibility of Java and Android classes was also solved. It is important to set the static final long serialVersionUID hard, because deserialization is very sensitive to the compiler, so the serialVersionUID will never match the serialized Java class in the sink version. - torin.dmitry