Example usage for twitter4j StatusUpdate setMedia

List of usage examples for twitter4j StatusUpdate setMedia

Introduction

In this page you can find the example usage for twitter4j StatusUpdate setMedia.

Prototype

public void setMedia(File file) 

Source Link

Usage

From source file:com.mysportsshare.mysportscast.utils.HelperMethods.java

License:Apache License

public static void postToTwitterWithImage(Context context, final Activity callingActivity,
        final String imageUrl, final String message, final TwitterCallback postResponse) {
    SharedPreferences mSharedPref = context.getSharedPreferences("Android_Twitter_Preferences",
            Context.MODE_PRIVATE);
    if (mSharedPref.getString(Constants.KEY_TWITTER_ACCESS_TOKEN, null) == null) {
        postResponse.onFinsihed(false);//from w ww .  j  a  va 2s . c  o m
        return;
    }

    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
    configurationBuilder.setOAuthConsumerKey(HomeActivity.CONSUMER_KEY);
    configurationBuilder.setOAuthConsumerSecret(HomeActivity.CONSUMER_SECRET);
    configurationBuilder.setOAuthAccessToken(HomeActivity.getAccessToken((context)));
    configurationBuilder.setOAuthAccessTokenSecret(HomeActivity.getAccessTokenSecret(context));
    Configuration configuration = configurationBuilder.build();
    final Twitter twitter = new TwitterFactory(configuration).getInstance();

    final File file = new File(imageUrl);

    new Thread(new Runnable() {

        @Override
        public void run() {
            boolean success = true;
            try {
                if (file.exists()) {
                    StatusUpdate status = new StatusUpdate(message);
                    status.setMedia(file);
                    twitter.updateStatus(status);
                } else {
                    Log.d(TAG, "----- Invalid File ----------");
                    success = false;
                }
            } catch (Exception e) {
                e.printStackTrace();
                success = false;
            }

            final boolean finalSuccess = success;

            callingActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    postResponse.onFinsihed(finalSuccess);
                }
            });

        }
    }).start();
}

From source file:com.tweet.HelperMethods.java

License:Apache License

public static void postToTwitterWithImage(Context context, final Activity callingActivity, final String message,
        final TwitterCallback postResponse) {
    if (!LoginActivity.isActive(context)) {
        postResponse.onFinsihed(false);//ww w . j a  va  2  s  . c o  m
        return;
    }

    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
    configurationBuilder.setOAuthConsumerKey(context.getResources().getString(R.string.twitter_consumer_key));
    configurationBuilder
            .setOAuthConsumerSecret(context.getResources().getString(R.string.twitter_consumer_secret));
    configurationBuilder.setOAuthAccessToken(LoginActivity.getAccessToken((context)));
    configurationBuilder.setOAuthAccessTokenSecret(LoginActivity.getAccessTokenSecret(context));
    Configuration configuration = configurationBuilder.build();
    final Twitter twitter = new TwitterFactory(configuration).getInstance();

    //final File file = new File(imageUrl);

    new Thread(new Runnable() {

        @Override
        public void run() {
            boolean success = true;
            try {

                if (StaticData.graphFile.exists()) {
                    Calendar cal = Calendar.getInstance();

                    StatusUpdate status = new StatusUpdate(message + "\n@" + cal.getTime().toString());
                    status.setMedia(StaticData.graphFile);
                    twitter.updateStatus(status);
                } else {
                    Log.d(TAG, "----- Invalid File ----------");
                    success = false;
                }
            } catch (Exception e) {
                e.printStackTrace();
                success = false;
            }

            final boolean finalSuccess = success;

            callingActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    postResponse.onFinsihed(finalSuccess);
                }
            });

        }
    }).start();
}

From source file:Controller.DetailsmesoffresController.java

@FXML
void partageTwitter(ActionEvent event) throws TwitterException, FileNotFoundException {

    OffreService offreService = new OffreService();
    FXMLAfficheroffresController z = new FXMLAfficheroffresController();

    Offre x = offreService.findById(Integer.parseInt(FXMLAfficheroffresController.getLabelid()));

    ConfigurationBuilder cb = new ConfigurationBuilder();

    cb.setDebugEnabled(true).setOAuthConsumerKey("zFrvXiT4O6bPh7wtfX2qpxTJM")
            .setOAuthConsumerSecret("pZeG2yJ71HYSu3y8MC4sbOcAeKstRKmp8bVKfCOmxnd4QjsKmC")
            .setOAuthAccessToken("3345656831-F533Ns9kdMPI6GfmHwiVor3BUHM6kYJn4WO4xhq")
            .setOAuthAccessTokenSecret("1hWrntZlel0gOju0xOEVf2t5kRo2CjSp5H75sieOUfT89");

    TwitterFactory tf = new TwitterFactory(cb.build());

    twitter4j.Twitter tw = tf.getInstance();

    String pathphoto = x.getPhoto();
    String nomphoto = pathphoto.substring(62);
    System.out.println(nomphoto);

    File file = null;//from  w w  w .  ja  v  a  2s  .c o m
    file = new File(x.getPhoto());

    String StatusMessage = (x.getPtvente().getNom() + "\n" + x.getDescription() + "\n" + x.getTaux_remise());

    StatusUpdate status = new StatusUpdate(StatusMessage);
    status.setMedia(file);
    tw.updateStatus(status);

}

From source file:Controller.DetailsOffreController.java

@FXML
void partagersurtwitter(ActionEvent event) throws TwitterException {

    OffreService offreService = new OffreService();
    AfficherToutesOffresController z = new AfficherToutesOffresController();

    Offre x = offreService.findById(Integer.parseInt(AfficherToutesOffresController.getLabelid()));

    ConfigurationBuilder cb = new ConfigurationBuilder();

    cb.setDebugEnabled(true).setOAuthConsumerKey("zFrvXiT4O6bPh7wtfX2qpxTJM")
            .setOAuthConsumerSecret("pZeG2yJ71HYSu3y8MC4sbOcAeKstRKmp8bVKfCOmxnd4QjsKmC")
            .setOAuthAccessToken("3345656831-F533Ns9kdMPI6GfmHwiVor3BUHM6kYJn4WO4xhq")
            .setOAuthAccessTokenSecret("1hWrntZlel0gOju0xOEVf2t5kRo2CjSp5H75sieOUfT89");

    TwitterFactory tf = new TwitterFactory(cb.build());

    twitter4j.Twitter tw = tf.getInstance();

    String pathphoto = x.getPhoto();
    String nomphoto = pathphoto.substring(62);
    System.out.println(nomphoto);

    File file = null;/*  ww  w.j a v  a 2  s .  c o m*/
    file = new File(x.getPhoto());

    String StatusMessage = (x.getPtvente().getNom() + "\n" + x.getDescription() + "\n" + x.getTaux_remise());

    StatusUpdate status = new StatusUpdate(StatusMessage);
    status.setMedia(file);
    tw.updateStatus(status);

}

From source file:gohai.simpletweet.SimpleTweet.java

License:Apache License

/**
 *  Tweet an image/*from   w w  w . j a v  a2 s  .c o  m*/
 *  @param img PImage instance to attach
 *  @param update twitter4j StatusUpdate instance to use
 *  @return partial URL of the tweet (e.g. "mrgohai/status/841086396194529280")
 */
public String tweetImage(PImage img, StatusUpdate update) {
    File temp;

    // write image into temporary file that gets deleted when the VM exits
    try {
        // make sure one pixel is somewhat transparent
        // to prevent Twitter from recompressing the image
        // to JPEG
        PImage copy = new PImage(img.width, img.height, PImage.ARGB);
        copy.copy(img, 0, 0, img.width, img.height, 0, 0, img.width, img.height);
        copy.set(0, 0, 254 << 24 | (copy.get(0, 0) & 0xffffff));
        temp = File.createTempFile("out", ".png");
        temp.deleteOnExit();
        copy.save(temp.getCanonicalPath());
    } catch (IOException e) {
        throw new RuntimeException("Error exporting image to PNG");
    }

    try {
        update.setMedia(temp);
        Status status = twitter.updateStatus(update);
        temp.delete();

        User user = status.getUser();
        return user.getScreenName() + "/status/" + status.getId();
    } catch (TwitterException e) {
        System.err.println(e.getMessage());
        throw new RuntimeException("Error posting tweet");
    }
}

From source file:main.RelativeTwitter.java

License:Creative Commons License

public static void tweet() {

    Frame.tweetStatsLabel.setForeground(Color.red);
    Frame.tweetStatsLabel.setText("?????????????");

    StatusUpdate statusUpdate = new StatusUpdate(
            Frame.bodyTextArea.getText() + " " + Frame.tagsTextArer.getText());

    ImageIcon postimg = new ImageIcon(FileCheck.selectedImg().getPath());
    Image instImg = postimg.getImage();
    BufferedImage thmb = new BufferedImage(instImg.getWidth(null), instImg.getHeight(null),
            BufferedImage.TYPE_INT_RGB);
    Graphics g = thmb.getGraphics();
    g.drawImage(instImg, 0, 0, null);/*  w  w  w . jav  a2s  . c  o  m*/

    File resizedImg;

    try {

        resizedImg = File.createTempFile("temp", ".jpg");
        ImageIO.write(thmb, "JPG", resizedImg);

        statusUpdate.setMedia(resizedImg);
    } catch (IOException e2) {
        e2.printStackTrace();
    }

    try {
        Frame.twitter.updateStatus(statusUpdate);

        Frame.tweetStatsLabel.setForeground(Color.BLUE);
        Frame.tweetStatsLabel.setText("???????");
    } catch (TwitterException e1) {
        Frame.tweetStatsLabel.setForeground(Color.RED);
        Frame.tweetStatsLabel
                .setText("?????????????");
        e1.printStackTrace();
    }

}

From source file:net.lacolaco.smileessence.twitter.TweetBuilder.java

License:Open Source License

public StatusUpdate build() {
    StatusUpdate statusUpdate = new StatusUpdate(buildText());
    if (inReplyToStatusID >= 0) {
        statusUpdate.setInReplyToStatusId(inReplyToStatusID);
    }/*from  ww  w . java 2 s  . c  o m*/
    if (!TextUtils.isEmpty(mediaPath)) {
        File media = new File(mediaPath);
        if (media.exists()) {
            statusUpdate.setMedia(media);
        }
    }
    return statusUpdate;
}

From source file:org.fossasia.phimpme.share.twitter.HelperMethods.java

License:Apache License

public static void postToTwitterWithImage(Context context, final String imageUrl, final String message,
        final String token, final String secret, final TwitterCallback postResponse) {
    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
    configurationBuilder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
    configurationBuilder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
    configurationBuilder.setOAuthAccessToken(token);
    configurationBuilder.setOAuthAccessTokenSecret(secret);
    Configuration configuration = configurationBuilder.build();
    final Twitter twitter = new TwitterFactory(configuration).getInstance();

    final File file = new File(imageUrl);
    boolean success = true;
    if (file.exists()) {
        try {/*from ww w.j  a  va2 s.  co  m*/
            StatusUpdate status = new StatusUpdate(message);
            status.setMedia(file);
            twitter.updateStatus(status);
        } catch (TwitterException e) {
            e.printStackTrace();
            success = false;
        }
    } else {
        Log.d(TAG, "----- Invalid File ----------");
        success = false;
    }
    postResponse.onFinsihed(success);

}

From source file:org.jetbrains.webdemo.servlet.TwitterServlet.java

License:Apache License

private void tweet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    try {//from   w w  w  .j a  v a  2  s  .c  o  m
        Twitter twitter = (Twitter) request.getSession().getAttribute("twitter");
        RequestToken requestToken = (RequestToken) request.getSession().getAttribute("requestToken");
        String verifier = request.getParameter("oauth_verifier");
        twitter.getOAuthAccessToken(requestToken, verifier);

        String status = (String) request.getSession().getAttribute("tweetText");
        request.getSession().removeAttribute("tweetText");
        StatusUpdate statusUpdate = new StatusUpdate(status);

        String level = (String) request.getSession().getAttribute("kotlin-level");
        request.getSession().removeAttribute("level");
        statusUpdate.setMedia(
                new File(CommonSettings.WEBAPP_ROOT_DIRECTORY + "static/images/" + level + "level.gif"));

        twitter.updateStatus(statusUpdate);
        request.getSession().removeAttribute("requestToken");
        response.sendRedirect(request.getContextPath() + "/");
    } catch (Throwable e) {
        throw new ServletException(e);
    }
}

From source file:org.tweetalib.android.model.TwitterStatusUpdate.java

License:Apache License

public StatusUpdate getT4JStatusUpdate() {
    StatusUpdate statusUpdate = new StatusUpdate(mStatus);

    if (mInReplyToStatusId != null) {
        statusUpdate.setInReplyToStatusId(mInReplyToStatusId);
    }// w w w .jav  a  2 s .  c o  m

    if (mMediaFilePath != null) {
        try {
            statusUpdate.setMedia(getMediaFile(mMediaFilePath));
        } catch (IOException error) {
            error.printStackTrace();
        } catch (OutOfMemoryError error) {
            error.printStackTrace();
        }
    }

    return statusUpdate;
}