List of usage examples for twitter4j Twitter uploadMedia
UploadedMedia uploadMedia(File mediaFile) throws TwitterException;
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 www .j ava 2 s.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:twitter4j.examples.tweets.UploadMultipleImages.java
License:Apache License
/** * Usage: java twitter4j.examples.tweets.UploadMultipleImages [text] [file1] [file2] ... * * @param args message/*from ww w . java 2 s . 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); } }