Search code examples
androidimageapitwittertwitter4j

How to upload media to twitter from background of application without showing any UI to user except login screen once?


I have tried to visit twitter developer website but i was not getting proper answer from there as i tried different ways to solve this problem but i did found TweetComposer.Builder still i was not able to work with it.


Solution

  • i have found solution for my question i have downloaded and integrated a library from here with the help of this library i am able to post multiple media

    Step 1:

    first login to twitter by integrating library from this link according to guidance

    Step 2:

    After that you will be able to login with twitter and you will get two things 1)authToken.token and 2)authToken.secret store this for further use.

    Step 3:

    public void updateTwitterStatus() {
    
        new AsyncTask() {
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }
    
            @Override
            protected Object doInBackground(Object[] params) {
                try {
                    ConfigurationBuilder builder = new ConfigurationBuilder();
                    builder.setOAuthConsumerKey(getResources().getString("YOUR_TWITTER_CONSUMER_KEY"));
                    builder.setOAuthConsumerSecret(getResources().getString("YOUR_TWITTER_CONSUMER_SECRET"));
    
                    // Access Token
                    String access_token = sharedPreferences.getString("authToken.token", "");
    
                    // Access Token Secret
                    String access_token_secret = sharedPreferences.getString("authToken.secret", "");
                    twitter4j.auth.AccessToken accessToken = new twitter4j.auth.AccessToken(access_token, access_token_secret);
                    twitter4j.Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);
    
                    // Update status for single image upload with raw folder
                    StatusUpdate statusUpdate = new StatusUpdate("posted from my app");
                    InputStream is = getResources().openRawResource(R.raw.lakeside_view);
                    statusUpdate.setMedia("test.jpg", is);
    
                    // Update status for single image upload with file
                    /*String statusMessage = "posted from my app";
                    StatusUpdate statusUpdate = new StatusUpdate(statusMessage);
                    statusUpdate.setMedia(file);*/
    
                    // Update status for multiple images upload with file
                    /*String statusMessage = "Hey I am posting 2 images of xyz event";
                    File imagefile1 = new File(Environment.getExternalStorageDirectory()+"/images/image_1.jpg");
                    File imagefile2 = new File(Environment.getExternalStorageDirectory()+"/images/image_2.jpg");
    
                    long[] mediaIds = new long[2];
                    UploadedMedia media1 = twitter.uploadMedia(imagefile1);
                    mediaIds[0] = media1.getMediaId();
                    UploadedMedia media2 = twitter.uploadMedia(imagefile2);
                    mediaIds[1] = media2.getMediaId();
                    StatusUpdate statusUpdate = new StatusUpdate(statusMessage);
                    statusUpdate.setMediaIds(mediaIds);*/
    
                    // to post twit
                    twitter4j.Status response = twitter.updateStatus(statusUpdate);
                    Log.d("Status", response.getText());
                } catch (twitter4j.TwitterException e) {
                    Log.d("Failed to post!", e.getMessage());
                }
                return null;
            }
    
            @Override
            protected void onPostExecute(Object o) {
                *//* Dismiss the progress dialog after sharing *//*
                Toast.makeText(getApplicationContext(), "Posted to Twitter!", Toast.LENGTH_SHORT).show();
                super.onPostExecute(o);
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }
    

    i have tried this code it works for me.