Android Question what is the best way to load colors from a file

Discussion in 'Android Questions' started by Cableguy, Feb 16, 2015.

  1. Cableguy

    Cableguy Expert Licensed User

    Hi guys

    What is the best way to deal with reading/writing color values from/to a file?
    I recon the best approach is to convert them to integers before writing and to use them as integers when reading... what do you usually do?
  2. Peter Simpson

    Peter Simpson Expert Licensed User

    Sounds good to me, then you can save them to a .dat RandomAccessFile.
    Hmm but saying that, I save each R, G, B value separately for my TIX Clock widget before saving the values in the file. I then read the separate R, G, B values when the widget first starts and I put the values back into their original places.

    DAAAAAAAAAAAAAAAM, I've just realised that you're member number four on this great community. What, did you join up the same day the site was released online???

    Why am I attempting to answer your question for, you're an expert. Ignore what I said about my app ;)
    Cableguy likes this.
  3. ivan.tellez

    ivan.tellez Active Member Licensed User

    It deppends on the amount of data to be store and frequency to be readed.

    For simple apps, saving INTs on a file should be enought.

    To store an read more colors, maybe SQLite.

    And for LOTS of colors to be readed, its better to use a data structure, Serialize and Deserialize to stored it in a file, or as a BLOB in a database.

    For this step you can use JSON to serializa and in memory compression to optimize writing and reading times.
  4. Erel

    Erel Administrator Staff Member Licensed User

    Check KeyValueStore class. Probably the best way.
  5. Cableguy

    Cableguy Expert Licensed User

    LoL... Actually I think I registered within the first 2 hours of the extinct release. But I was around before that, back when Erel was just starting to get b4ppc noticed by the masses, and the b4ppc forum was offered us by a comunity member called GeoTrail.
    I may be labeled as an Expert, but I am way more close to a noob than an Expert.

    Now, coming back to my question...
    What I plan to do is to allow theming an app I'm planning for my wife (you know how girls love to change things around), giving her the ability to change background and border colors of the apps views. The file will then be stored inside a theme folder, and only read once on app start...

    Dividing the colors in to its R;G;B components seems to be cumbersome to me... if I understand correctly, colors.RGB only converts the given components to an integer, so I can just feed this integer as a cor value.

    I was thinking on using either an array or map to store the color values... but I will check the KeyStore...
    Last edited: Feb 17, 2015
    Erel likes this.
  6. Erel

    Erel Administrator Staff Member Licensed User

    Colors are integers. You don't need to do anything special to convert a color to int.

    Peter Simpson likes this.
  7. Peter Simpson

    Peter Simpson Expert Licensed User

    Hey @Cableguy, you were an early adapter of b4ppc, good on you :)
    Yes they are integers thus can be stored simply enough, in my case all my colours can be user customised, I just stored them separately as I'm using ColorDialog and I just wanted to do it that way. If my memory serves me correctly, cont all the colour integers start with a (-)hyphen. I remember doing ToastMessageShow(Colors.Black, False) and ToastMessageShow(Colors.RGB(0, 0, 0), False) thinking that it would return 000000 or 0, but infact it returned some strange -16xxxx number, so I saved the RGB values separately instead, that was just me though.

    @Erel you have told user a few times now to use the KeyValueStore Class when storing values. I've never ever looked into that class so maybe on my next project I will look into that class, but I personally like storing values in an encrypted RandomAccessFile. you obviously know more about these things than anybody else as you wrote them. I will have to look into KeyValueStore to see what the benefits are compared to using RandomAccessFile if any.
  8. Cableguy

    Cableguy Expert Licensed User

    SInce I will be establishing the themes myself, I may take the easy route and just write the output of colors.rgb (xxx,xxx,xxx)
    You should also keep in mind when dealing with colors, that we usually use solid colors, but code wise, they just have an alfa of 255!
  9. Cableguy

    Cableguy Expert Licensed User

  10. Erel

    Erel Administrator Staff Member Licensed User

    KeyValueStore is based on RandomAccessFile. It just makes it simpler to use.
  11. Peter Simpson

    Peter Simpson Expert Licensed User

    Dam it of course I never even though about the Alpha being defaulted to 255, that's obviously why. Obviously using Colors.ARGB and setting the Alpha to 0 will bring back 0 as I originally expected. I just remember trying RGB 0, 0, 0 and not getting 0 in return, but in return getting -16xxxx integer. So I just saved the RGB values separately as I was in a hurry to finish the app and didn't have time to look into it fully. Anyway I'm pleased that I did it that way as I learned about other things instead :).

    Wow you learn something new every single day on this great community, and as I stated earlier @Cableguy.
    Sorry :p
    RandomCoder likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice