Quantcast
Channel: Tweetinvi a friendly Twitter C# library
Viewing all articles
Browse latest Browse all 4126

New Post: Uploading tweet with media

$
0
0
Hi,

Here is some code that I adapted from the internet to add an image to the isolated storage of the Windows Phone Emulator:
publicstaticclass EmulatorHelper
{
    conststring flagName = "__emulatorTestImagesAdded";

    publicstaticvoid AddDebugImages()
    {
        bool alreadyAdded = CheckAlreadyAdded();
        if (!alreadyAdded)
        {
            AddImages();
            SetAddedFlag();
        }
    }

    privatestaticbool CheckAlreadyAdded()
    {
        IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;

        try
        {
            bool alreadyAdded = (bool)userSettings[flagName];
            return alreadyAdded;
        }
        catch (KeyNotFoundException)
        {
            returnfalse;
        }
        catch (ArgumentException)
        {
            returnfalse;
        }
    }

    privatestaticvoid SetAddedFlag()
    {
        IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;
        userSettings.Add(flagName, true);
        userSettings.Save();
    }

    privatestaticvoid AddImages()
    {
        string[] fileNames = { "salut" };
        foreach (var fileName in fileNames)
        {
            MediaLibrary myMediaLibrary = new MediaLibrary();
            Uri myUri = new Uri(String.Format(@"TestImages/{0}.jpg", fileName), UriKind.Relative);

            System.IO.Stream photoStream = App.GetResourceStream(myUri).Stream;
            byte[] buffer = newbyte[photoStream.Length];
            photoStream.Read(buffer, 0, Convert.ToInt32(photoStream.Length));
            myMediaLibrary.SavePicture(String.Format("{0}.jpg", fileName), buffer);
                
            photoStream.Close();
        }
    }
}
Here is a method to get a byte[] from a file stored in the isolated storage of the Windows Phone.
publicbyte[] ReadFile(String fileName)
{
    byte[] bytes;
    IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
    var filenames = storage.GetFileNames();

    using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream file = appStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read))
        {
            bytes = newbyte[file.Length];

            var count = 1024;
            var read = file.Read(bytes, 0, count);
            var blocks = 1;
            while (read > 0)
            {
                var remainingBytes = file.Length - blocks*count;
                var nbBytesToAdd = (int)(remainingBytes > count ? count : remainingBytes);

                read = file.Read(bytes, blocks * count, nbBytesToAdd);

                if (remainingBytes <= count)
                {
                    break;
                }

                blocks += 1;
            }
        }
    }
    return bytes;
}
I did not investigate further but for some reason if you want to access the "salut" file please use the following line:
var file = ReadFile("salut_jpg.jpg");
Please let me know if it helps.

Regards,
Linvi

Viewing all articles
Browse latest Browse all 4126

Trending Articles