Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Save Photo in Playerpref

Discussion in 'Editor & General Support' started by amirivala, Nov 13, 2014.

  1. amirivala

    amirivala

    Joined:
    May 29, 2013
    Posts:
    56
    Hi, Im new to unity coding, and I've been searching how to save a photo in playerPref but I've found that you cannot put photo inside the PlayerPref.
    What I've trying to do is to get the photo from internet, and store it inside app and every time i can load it even in offline Mode ( Actully saving the photo for ProfiePicture inside the app)
    Ive also found this article on Internet:

    http://forum.unity3d.com/threads/loading-an-image-from-www-can-i-save-it-in-playerprefs.215530/

    but that post only shows how to save and i don't have any idea how to load it back inside my material texture

    Please help me

    Thank you, Appreciate it
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    PlayerPrefs is not the ideal method for saving photos to disk. Part of the reason is that on some platforms there is a limit to the amount of data that the PlayerPrefs can save. It would be a lot wiser to write the texture straight to the Application.persistantDataPath directory. This is a pretty straight-forward process, first convert the image to a byte array (EncodeToPNG/EncodeToJPG). Next make a call to File.WriteAllBytes(Application.persistantDataPath + "/saved image.png"). And that's it for the writing. For the reading you first call File.ReadAllBytes(). Then create a Texture2D and call LoadImage(theBytes). Done.
     
    amirivala likes this.
  3. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    If the app is a web player, then it has zero access to the hard drive.
     
    amirivala likes this.
  4. amirivala

    amirivala

    Joined:
    May 29, 2013
    Posts:
    56
    So Ive tried what you've said but still not working can you tell me where is my problem?
    I'm using prime31 Social network to get the profile picture:

    Code (CSharp):
    1. Facebook.instance.fetchProfileImageForUserId (_userId, ( tex ) =>
    2.                                                       {
    3.             bytes = tex.EncodeToPNG();
    4.             File.WriteAllBytes( Application.persistentDataPath + "/saved image.png", bytes );
    5.  
    6.         });
    and after that I'm trying to load it from another code :

    Code (CSharp):
    1. void OnClick () {
    2.  
    3.         bytes = File.ReadAllBytes (Application.persistentDataPath + "/saved image.png");
    4.         twoDtex.LoadImage (bytes);
    5.         tex.mainTexture = twoDtex;
    6.     }
    but it won't work and console says:
    NullRefrenceException: object refrence not set to an instance of an object

    Thank you please guide me
     
    CrandellWS likes this.
  5. amirivala

    amirivala

    Joined:
    May 29, 2013
    Posts:
    56
    I'm using it for iOS and Android.
     
  6. amirivala

    amirivala

    Joined:
    May 29, 2013
    Posts:
    56
    Ive found where was the problem, i have to use path combine for the directory:
    Code (CSharp):
    1. var fileName = Path.Combine( Application.persistentDataPath, "photoName.png" );
     
    CrandellWS likes this.
  7. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    So you're saying that he should use PlayerPrefs to save images?
     
  8. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Don't think I said that. If you're playing a web browser game, and want it to work offline you're going to have a lot of work to do, and probably require that the data you need is cached in the browser cache.