安卓twitter4j上传图片

10

我有一个安卓应用程序,可以成功地更新 Twitter 状态。我正在使用它来分享链接到一个显示图片的网页。我认为,与 Instagram 类似,在推文中添加一个缩小的图片预览将会很不错。我该如何在推文中上传这张图片?


尝试使用更新的twitter4j jar,链接为http://stackoverflow.com/questions/17093499/how-to-post-image-to-twitter-in-android/20633178#20633178 - Prachi
1个回答

43

我曾经有同样的需求,最终我创建了这个函数,也许它对你也有帮助。

/**
 * To upload a picture with some piece of text.
 * 
 * 
 * @param file The file which we want to share with our tweet
 * @param message Message to display with picture
 * @param twitter Instance of authorized Twitter class
 * @throws Exception exception if any
 */

public void uploadPic(File file, String message,Twitter twitter) throws Exception  {
    try{
        StatusUpdate status = new StatusUpdate(message);
        status.setMedia(file);
        twitter.updateStatus(status);}
    catch(TwitterException e){
        Log.d("TAG", "Pic Upload error" + e.getErrorMessage());
        throw e;
    }
}

注意:要使用此功能,请确保您拥有我使用的最新jar文件twitter4j-core-2.2.5.jar


5
好的!非常感谢。完美地解决了问题! :) - Rameez Hussain
在你建议我将Twitter jar从2.i.i替换为2.2.5之后,我遇到了新的错误:AndroidRuntime(7452):java.lang.IllegalArgumentException: Invalid access token format。以前我能够发布推文,但只是尝试上传图片时出现问题。 - mrYogi
请问Twitter是什么? - yakusha
1
Twitter是从Twitter4j库中使用的对象。 - Salman Khakwani
1
如果我想分享一个链接怎么办? - KarenAnne
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接