There is an object that you want to write to the file.

An object consists of the following fields:

private Organization organization; private int status = -1; private Drawable photo; private ByteArrayInputStream avatar; 

when trying to serialize, I get an error:

 java.io.NotSerializableException: java.io.ByteArrayInputStream java.io.NotSerializableException: java.io.BitmapDrawable 

Tell me how can you save such an object to a file? Or into which you can convert images (mostly Drawable), so that they can be saved.

    2 answers 2

    1. Drawable store in resources.

    2. ByteArrayInputStream cannot be serialized as an absolutely abstract class, like all its methods, except its state configs. Just think about what you want to get after deserializing the Object of this class? Exactly - nothing, this is the buffer for the InputStream . As a solution, mark it transient, when deserializing, it will have null .

    • and how can you save bitmap to resources? - Kirill Stoianov
    • Yes, Drawable, of course, BitMap is just its simplest form, but the essence is the same - Shwarz Andrei
    • Yes, I need a Drawable, as you can save it at the time of execution to the resource folder, I understand that this will not work - Kirill Stoianov
    • And in this question, well, work with BitMap-> File) save to the resource folder. I think there are many implementations, just what came to mind. - Shwarz Andrei
    • one
      @KirillStoianov, the author has in mind, apparently, that you need to either store or res/drawable images in the object, operating only with their id in the R class or, if you res/drawable them from the network and are not static at all, then you need them You create some folder you created on the device in the files you created. And at serialization to store not Drawable and the link to the file. - YurySPb

    Make sure that all the fields of the object being serialized are also serializable. If at least one field is not Serializable, then nothing will work.

    • The last two fields in the object are not serializable, I thought it was obviously based on the exceptions that I received, the question is how to fix it? - Kirill Stoianov
    • Drawable can be replaced with a string with the name of the image and, after deserialization, to receive a picture from resources. How to replace ByteArrayInputStream, I do not know, I will have to help. By the way, ALL objects of the real world can be described by primitives and arrays (lists) of these primitives. If you try to serialize something else, then you are 100% doing something wrong - P. Ilyin
    • Those. do you propose not to serialize Drawable to a file, but instead to save it in res / drawable, but to write its name in a serializable object? right? This option doesn't suit me because I need to save pictures dynamically - Kirill Stoianov
    • With each new message I find out more about your task, more and more, I pass - P. Ilyin
    • The task is one, it is described in the question, you need to save the object that contains Drawable or separately as it is to save Drawable - Kirill Stoianov