Uploading tweet with media

Jan 10, 2014 at 1:43 PM
Hi, is there a functionality to tweet using tweetinvi with media files such as pictures?
Coordinator
Jan 10, 2014 at 3:04 PM
Hi Chagina,

Unfortunately this feature is not yet available within Tweetinvi because it is more complicated than any other feature of the Twitter REST API.
The development of this feature is though planned.

Please feel free to vote on the feature, I always try to prioritize the features with the most votes.

Regards,
Linvi
Jan 10, 2014 at 3:22 PM
Thanks for the reply linvi, i'll make sure to vote for it, but meanwhile please take it into consideration, i believe it'll make tweetinvi a complete package...
Coordinator
Jan 10, 2014 at 3:35 PM
I know this is an important feature and I will try to implement sooner than later.
In the meantime I currently have so much work with Tweetinvi 0.9 that I cannot implement this feature right away.

I hope you'll understand.
Mar 21, 2014 at 2:26 PM
Hi, just want to know is this feature already impelemented? i looking for a way to upload picture along with tweet in windows phone apps and until now i can't find any way to make it works in other library and hoping maybe yours can do that?
Coordinator
Mar 21, 2014 at 2:34 PM
Hi,

Not yet but... I am currently working on 0.9.1.0 which is migration to PCL, this is almost done and the API will consequently work on WP8.
And the next release 0.9.2.0 is only about uploading photo in addition to a tweet.
As I will be working on this in the coming week I suggest that you keep a look around.

Regards,
Linvi
Mar 21, 2014 at 2:41 PM
okay i looking foward for your next release :)
Coordinator
Apr 9, 2014 at 12:00 AM
Hi Tonizz,

I am happy to let you know that Tweetinvi is now supporting Windows Phone 8 as well as the publication of media.
var file = ReadFile("my_file.jpg"); // Method to extract byte[] from a file on WP
var t = Tweet.CreateTweetWithMedia("hello guys", file);
var success = await t.PublishAsync();
Hope this serves you well.
Regards,
Linvi
Marked as answer by linvi on 4/9/2014 at 4:42 PM
Apr 23, 2014 at 9:20 AM
hi thank for the reply, and i spend a week to figure out how to extract byte[] from a file... mind to sharing it to me? i just don't have any idea how to do that....
Coordinator
Apr 24, 2014 at 12:01 AM
Edited Apr 24, 2014 at 12:05 AM
Hi,

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

    public static void AddDebugImages()
    {
        bool alreadyAdded = CheckAlreadyAdded();
        if (!alreadyAdded)
        {
            AddImages();
            SetAddedFlag();
        }
    }

    private static bool CheckAlreadyAdded()
    {
        IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;

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

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

    private static void 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 = new byte[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.
public byte[] 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 = new byte[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 note that you need to add some authorizations to your Windows Phone app (WMAppManifest.xml):
I personally have the following:
  • ID_CAP_MEDIALIB_PHOTO
  • ID_CAP_NETWORKING
  • ID_CAP_REMOVABLE_STORAGE
Please let me know if it helps.

Regards,
Linvi
May 16, 2014 at 5:06 AM
I always get these error while using PublishTweetWithMedia.
POST : https://api.twitter.com/1.1/statuses/update_with_media.json
...
An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary
System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
   at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClasse.<EndGetResponse>b__d(Object sendState)
   at System.Net.Browser.AsyncHelper.<>c__DisplayClass1.<BeginOnUI>b__0(Object sendState)
   --- End of inner exception stack trace ---
   at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
   at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at Tweetinvi.WebLogic.TwitterRequestGenerator.MultipartWebRequest.<>c__DisplayClass6.<>c__DisplayClass8.<GetResult>b__5(IAsyncResult a)
this is my script:
byte[] imageData = ReadFile(imageFileName);
if(imageData != null && imageData.Length > 0){
    TwitterCredentials.SetCredentials(token, tokensecret, TwitterSettings.ConsumerKey, TwitterSettings.ConsumerKeySecret);
    await TweetAsync.PublishTweetWithMedia("tes #GIFMe", imageData);
    //await TweetAsync.PublishTweet("tes #GIFMe");
}
The PublishTweet is working fine, but the PublishTweetWithMedia is not.
I use ReadFile function from your previous comment to read image from isolated storage.
May 16, 2014 at 7:04 AM
Finally, i know what is the cause of the problem.
I try to upload animated GIF image, that is not supported by twitter :(

anyway, thanks for the great library.