Save Photo in Playerpref

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:

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

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.

1 Like

If the app is a web player, then it has zero access to the hard drive.

1 Like

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:

Facebook.instance.fetchProfileImageForUserId (_userId, ( tex ) =>
                                                      {
            bytes = tex.EncodeToPNG();
            File.WriteAllBytes( Application.persistentDataPath + "/saved image.png", bytes );

        });

and after that I’m trying to load it from another code :

void OnClick () {

        bytes = File.ReadAllBytes (Application.persistentDataPath + "/saved image.png");
        twoDtex.LoadImage (bytes);
        tex.mainTexture = twoDtex;
    }

but it won’t work and console says:
NullRefrenceException: object refrence not set to an instance of an object

Thank you please guide me

1 Like

I’m using it for iOS and Android.

Ive found where was the problem, i have to use path combine for the directory:

var fileName = Path.Combine( Application.persistentDataPath, "photoName.png" );
1 Like

So you’re saying that he should use PlayerPrefs to save images?

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.