Save bytearray (encrypted password) as Picture using medialibrary WP8

181 Views Asked by At

im really confused with some storage functionalities on Windows Phone 8. Im trying to save credentials (password/user), which are shared by several apps, in the local phone storage. From there the user will be able to change the password only once in one app and it will change for all apps. i have been through 3 possibilities

  1. Save on external server
  2. Save on Medialibrary as a picture
  3. Save on the Isolatedstorage of a new specific app which manages the credentials

In this code which expose the 2nd case i get an issue "System.InvalidOperationException.An unexpected error has occurred"

    public static void SaveToFile(byte[] Encryptedfile, string FileName)
    {      
        using (var mediaLibrary = new MediaLibrary())
        {
            using (var stream = new MemoryStream(Encryptedfile))
            {
                    var file = string.Format(FileName, Guid.NewGuid());
                    stream.Seek(0, SeekOrigin.Begin);
                    var picture = mediaLibrary.SavePicture(file, stream);  //ERROR
            }
        }
    }

the call of the function

        byte[] PasswordByte = Encoding.UTF8.GetBytes(password);
        byte[] UserByte = Encoding.UTF8.GetBytes(user);

        byte[] EncryptedPasswordUser = ProtectedData.Protect(PasswordByte, null);
        byte[] EncryptedUser = ProtectedData.Protect(UserByte, null);


        IsolatedStorageOperations.SaveToFile(EncryptedPasswordUser, "Password");
        IsolatedStorageOperations.SaveToFile(EncryptedUser, "User");

I would be happy if you can give me another way to save files in a common local storage on WP8 or if you can give me a solution to the issue with the medialibrary.

thanks

1

There are 1 best solutions below

1
On

Use WriteableBitmap to save image in Media library. May this will help you

public static void SaveToFile(byte[] Encryptedfile, string FileName)
     {
       using (var stream = new MemoryStream(Encryptedfile))
         {
         var file = string.Format(FileName, Guid.NewGuid());
         WriteableBitmap bitmap = new WriteableBitmap(100,100);
         bitmap.SaveJpeg(stream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100);
         stream.Seek(0, SeekOrigin.Begin);
         using (MediaLibrary mediaLibrary = new MediaLibrary())
          mediaLibrary.SavePicture(file , stream);
         MessageBox.Show("Image saved");
         }
     }