List of usage examples for twitter4j StatusUpdate setMediaIds
public void setMediaIds(long... mediaIds)
From source file:au.com.tyo.services.sn.twitter.SNTwitter.java
License:Apache License
private Status postTweet(Tweet tweet) throws TwitterException { StatusUpdate what = new StatusUpdate(tweet.getText()); if (tweet.hasMedia()) what.setMediaIds(tweet.getMediaIds()); return twitter.updateStatus(what); }
From source file:au.com.tyo.sn.twitter.SNTwitter.java
License:Apache License
public Status postTweet(Tweet tweet, String[] mediaUrls) throws TwitterException { StatusUpdate what = new StatusUpdate(tweet.getText()); if (null == tweet.getMediaIds() && null != mediaUrls && mediaUrls.length > 0) { for (int i = 0; i < 4 && i < mediaUrls.length; ++i) { UploadedMedia media = null;//from w ww . j a v a2s .com String imgUrl = mediaUrls[i]; Uri uri = Uri.parse(imgUrl); String imgTitle = uri.getLastPathSegment(); try { URL url = new URL(imgUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); InputStream is = url.openStream(); /* * can't do it, it is deprecated */ //what.setMedia(imgTitle, is); media = twitter.uploadMedia(imgTitle, is); } catch (Exception ex) { /* * Something wrong, but that is ok, just ignore it */ ex.printStackTrace(); } finally { if (null != media) { tweet.setMediaId(media.getMediaId()); } } } } if (null != mediaUrls) what.setMediaIds(tweet.getMediaIds()); return twitter.updateStatus(what); }
From source file:com.github.moko256.twitlatte.model.impl.twitter.PostTweetModelImpl.java
License:Apache License
@Override public Completable postTweet() { return Completable.create(subscriber -> { try {//w w w . ja va2 s . c o m StatusUpdate statusUpdate = new StatusUpdate(tweetText); if (uriList.size() > 0) { long ids[] = new long[uriList.size()]; for (int i = 0; i < uriList.size(); i++) { Uri uri = uriList.get(i); InputStream image = contentResolver.openInputStream(uri); ids[i] = twitter.uploadMedia(uri.getLastPathSegment(), image).getMediaId(); } statusUpdate.setMediaIds(ids); statusUpdate.setPossiblySensitive(possiblySensitive); } if (isReply()) { statusUpdate.setInReplyToStatusId(inReplyToStatusId); } if (location != null) { statusUpdate.setLocation(location); } twitter.updateStatus(statusUpdate); subscriber.onComplete(); } catch (FileNotFoundException | TwitterException e) { subscriber.tryOnError(e); } }); }
From source file:com.marpies.ane.twitter.functions.UpdateStatusFunction.java
License:Apache License
private void uploadMedia(final AsyncTwitter twitter, final StatusUpdate status, final List<File> mediaList, final Integer callbackID) { /* Run the upload in separate thread */ new Thread(new Runnable() { @Override//from ww w. j a v a 2s. c o m public void run() { final long[] mediaIDs = new long[mediaList.size()]; try { /* Synchronous twitter4j must be used in order to upload the media */ Twitter syncTwitter = TwitterAPI.getInstance(TwitterAPI.getAccessToken()); for (int i = 0; i < mediaIDs.length; i++) { File mediaFile = mediaList.get(i); AIR.log("Uploading media " + mediaFile.getAbsolutePath()); UploadedMedia uploadedMedia = syncTwitter.uploadMedia(mediaFile); mediaIDs[i] = uploadedMedia.getMediaId(); } // todo: remove cached media files status.setMediaIds(mediaIDs); /* Update status only if there is no exception during media upload */ twitter.updateStatus(status); } catch (TwitterException e) { AIR.log("Error uploading media " + e.getMessage()); /* Failed to upload media, let's not update status without the media */ AIR.dispatchEvent(AIRTwitterEvent.STATUS_QUERY_ERROR, StringUtils.getEventErrorJSON(callbackID, StringUtils.removeLineBreaks(e.getMessage()))); } } }).start(); }
From source file:kerguelenpetrel.UpdateStatusServlet.java
License:Apache License
@Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { StringBuilder builder = new StringBuilder(); resp.setContentType("text/plain; charset=UTF-8"); resp.getWriter().println("Updating status..."); try {/*from w w w .jav a2 s . c o m*/ //Append feed title builder.append(getFeedTitle(resp)); //Append Wordnik example sentence builder.append(getWordnikSentence(resp)); /* Tweets are maximum 280 characters, so trim our sentence appropriately */ if (builder.length() > 280) { if (builder.lastIndexOf(";", 220) > 0) builder.setLength(builder.lastIndexOf(";", 220)); else if (builder.lastIndexOf(":", 220) > 0) builder.setLength(builder.lastIndexOf(":", 220)); else if (builder.lastIndexOf(",", 220) > 0) builder.setLength(builder.lastIndexOf(",", 220)); else builder.setLength(220); } //Append a Global trend Twitter twit = TwitterFactory.getSingleton(); builder.append( " " + twit.getPlaceTrends(1).getTrends()[r.nextInt(twit.getPlaceTrends(1).getTrends().length)] .getName()); // Append a Wordnik trend builder.append(getWordnikTrend(resp)); if (builder.length() > 280) builder.setLength(280); //Tweets are limited to 280 characters //Set the status StatusUpdate status = new StatusUpdate(builder.toString()); /* Add an image from Flickr for small status */ if (builder.length() < 180) status.setMediaIds(addFlickrImg(twit, resp)); twit.updateStatus(status); resp.getWriter().println("Tweet posted: " + status.getStatus()); } catch (TwitterException e) { resp.getWriter().println("Problem with Twitter \n"); e.printStackTrace(resp.getWriter()); } }
From source file:twitter4j.examples.tweets.UploadMultipleImages.java
License:Apache License
/** * Usage: java twitter4j.examples.tweets.UploadMultipleImages [text] [file1] [file2] ... * * @param args message/*w w w . j a v a 2s. c o m*/ */ public static void main(String[] args) { if (args.length < 1) { System.out.println( "Usage: java twitter4j.examples.tweets.UploadMultipleImages [text] [file1] [file2] ..."); System.exit(-1); } try { Twitter twitter = new TwitterFactory().getInstance(); long[] mediaIds = new long[args.length - 1]; for (int i = 1; i < args.length; i++) { System.out.println("Uploading...[" + i + "/" + (args.length - 1) + "][" + args[i] + "]"); UploadedMedia media = twitter.uploadMedia(new File(args[i])); System.out.println("Uploaded: id=" + media.getMediaId() + ", w=" + media.getImageWidth() + ", h=" + media.getImageHeight() + ", type=" + media.getImageType() + ", size=" + media.getSize()); mediaIds[i - 1] = media.getMediaId(); } StatusUpdate update = new StatusUpdate(args[0]); update.setMediaIds(mediaIds); Status status = twitter.updateStatus(update); System.out.println( "Successfully updated the status to [" + status.getText() + "][" + status.getId() + "]."); System.exit(0); } catch (TwitterException te) { te.printStackTrace(); System.out.println("Failed to update status: " + te.getMessage()); System.exit(-1); } }