我編寫了一個應用程序,可以發送一條消息給twitter並附帶一個圖片。有用!我在幾臺設備上進行了測試,並要求其他人也這樣做。它甚至適用於選擇Twitter朋友時的直接留言。但是,選擇「直接留言」時不起作用。這迫使用戶直接選擇一個朋友,而不是通過「直接信息」選擇他(這真的很奇怪),否則圖片沒有附加。只要看看截圖:如何將圖像附加到twitter Android中的「直接消息」?
這裏是我的Xamarin的Android程序代碼。讓我知道如何解決它。目前,所有選項都可以工作,甚至選擇我的朋友,但不是「直接留言」。我還需要告訴我,我不希望在推文中看到有關Twitter文本的任何問題。
public bool TweetImage(Bitmap imageToTweet)
{
var messageIntent = context.FindMessageIntent(this.twitterConstants.PackageName);
if (messageIntent == null)
{
return false;
}
string outputFileBMP = SaveBitmap(imageToTweet);
context.Tweet(messageIntent, outputFileBMP, this.twitterConstants.DefaultTwitterText, this.twitterConstants.ChooserMessage);
return true;
}
和
public static Intent FindMessageIntent(this ContextWrapper contextWrapper, params string[] packageNames)
{
Intent wantedIntent = new Intent();
wantedIntent.SetType("text/plain");
var resolveInfos = contextWrapper.PackageManager.QueryIntentActivities(wantedIntent, PackageInfoFlags.MatchDefaultOnly);
var result = (from r in resolveInfos
from p in packageNames
where p == r.ActivityInfo.PackageName
select p).FirstOrDefault();
if (result != null)
{
wantedIntent.SetPackage(result);
return wantedIntent;
}
return null;
}
和
public static void Tweet(this ContextWrapper contextWrapper, Intent messageIntent, string filePath = null, string message = null, string chooserMessage = null)
{
if (filePath != null)
{
using (var file = new Java.IO.File(filePath))
{
messageIntent.PutExtra(Intent.ExtraStream, Android.Net.Uri.FromFile(file));
}
}
if (message != null)
{
messageIntent.PutExtra(Intent.ExtraText, message);
}
if (chooserMessage != null)
{
using (var chooser = Intent.CreateChooser(messageIntent, chooserMessage))
{
contextWrapper.StartActivity(chooser);
}
return;
}
contextWrapper.StartActivity(messageIntent);
}
請注意,我用的Android和需要基於Android的解決方案(基於意圖)。
可能的重複[如何發送圖像的直接消息?](http://stackoverflow.com/questions/37345016/how-to-send-an-image-in-direct-message) –