Example usage for twitter4j UploadedMedia getMediaId

List of usage examples for twitter4j UploadedMedia getMediaId

Introduction

In this page you can find the example usage for twitter4j UploadedMedia getMediaId.

Prototype

public long getMediaId() 

Source Link

Usage

From source file:au.com.tyo.services.sn.twitter.SNTwitter.java

License:Apache License

/**
 *
 * @param tweet//from ww w.j ava2  s . c  o m
 * @param files
 * @return
 * @throws TwitterException
 */
public Status postTweet(Tweet tweet, File[] files) throws TwitterException {
    if (null != files && files.length > 0) {
        for (File file : files) {
            UploadedMedia media = twitter.uploadMedia(file);
            if (null != media)
                tweet.setMediaId(media.getMediaId());
        }
    }
    return postTweet(tweet);
}

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;
            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();

                /*//from   w w w  .  ja v  a2  s  . c  o  m
                 * 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.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 w  w  w .jav a2  s .c  om
        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.j a  v a2  s  .co  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);
    }
}