android - Saving an Object containing Bitmap -


i'm working on android application (first application-beginner) , i'm trying save data when app closes load again.

the data want store list of books, , each book contains info (author, date, etc) , bitmap containing book's picture. tried using gson convert list json , store in sharedpreferences caused problems because of bitmap.

how should save file , retrieve again when app launches ?

this brief version of code

library class

public class library {          private arraylist<entry> library ;          public library () {             library = new arraylist<entry>();         }          public void addentry( entry entry ) {             library.add(entry);         }          public void removeentry ( entry entry ) {             if (library.contains(entry))                 library.remove(entry);             else log.d ( "library" , "entry not found");         }          public arraylist<entry> getlibrary() {             return library;         }          @override         public string tostring() {             return "library{" +                     "library=" + library +                     '}';         } } 

entry class

public class entry {     book book ;     final localdate borrowdate;      localdate duedate;      //some methods application } 

book class

public class book implements parcelable {  private string title; private string author; private string isbn ; private double rating; private int ratingcount; private int pagecount; private transient bitmap image; private string overview; //some methods  } 

don't put bitmap in sharedpreferences. save in file.

if need persist bitmap, can assign static field.

you can convert 64 bit string.. bad design (and think slow , expensive operation!):

bytearrayoutputstream baos = new bytearrayoutputstream();   bm.compress(bitmap.compressformat.png, 100, baos); //bm bitmap object    byte[] b = baos.tobytearray(); string encoded = base64.encodetostring(b, base64.default);  

^ save sharedpreferece. decode:

byte[] imageasbytes = base64.decode(encoded.getbytes()); imageview image = (imageview)this.findviewbyid(r.id.imageview); image.setimagebitmap(bitmapfactory.decodebytearray(imageasbytes, 0, imageasbytes.length)); 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -