在twitter中通过url分享图片

时间:2014-04-03 08:46:33

标签: android twitter4j

这是代码

        StatusUpdate status = new StatusUpdate(msg);
        twitter.updateStatus(status);

它工作正常。      但我希望通过url

分享我的形象

请帮帮我。

4 个答案:

答案 0 :(得分:1)

请尝试以下操作:

  1. 获取您要上传的图片的路径(如果已在SD卡上)。如果没有,则首先下载,保存到SD卡,然后获取路径。

  2. 然后使用该文件路径(picFilePath)创建一个文件,如下所示:

     File imgFile = new File(picFilePath); 
    
  3. 将此文件设置为statusupdate对象中的媒体

     // the txt message  
     StatusUpdate status = new StatusUpdate(Msg);
     // set the image file as media with the message. 
     status.setMedia(imgFile);
    
  4. 使用AsyncTask

    上传带有图像的邮件
     twitter.updateStatus(status);
    
  5. 希望这会对你有所帮助。

答案 1 :(得分:0)

试试这个:

 T4JTwitterFunctions.postToTwitter
                (your_class.this.getApplicationContext(),your_class.this, twitter_consumer_key, twitter_consumer_secret, 
                Your_URL, new T4JTwitterFunctions.TwitterPostResponse() 
                { @Override public void OnResult(Boolean success) 
                    { 
                         if(success)
                         { 
                        //success
                         }
                         else
                         {
                        //not success
                         }
                    } 
                }); 

答案 2 :(得分:0)

我认为此代码可以帮助您:

class updateTwitterStatus extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Updating to twitter...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting Places JSON
     * */
    protected String doInBackground(String... args) {
        Log.d("Tweet Text", "> " + args[0]);
        String status = args[0];
        try {
            ConfigurationBuilder builder = new ConfigurationBuilder();
            builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
            builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);

            // Access Token 
            String access_token = mSharedPreferences.getString(PREF_KEY_OAUTH_TOKEN, "");
            // Access Token Secret
            String access_token_secret = mSharedPreferences.getString(PREF_KEY_OAUTH_SECRET, "");
            System.out.println(access_token+access_token_secret+"....."+PREF_KEY_OAUTH_TOKEN);
            AccessToken accessToken = new AccessToken(access_token, access_token_secret);
            Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);


            StatusUpdate ad=new StatusUpdate("mala ruparel...........");



                // The InputStream opens the resourceId and sends it to the buffer
                InputStream is = getResources().openRawResource(R.drawable.f1);

                ad.setMedia("Malvika",is);
            // Update status
            twitter4j.Status response = twitter.updateStatus(ad);




            Log.d("Status", "> " + response.getText());
        } catch (TwitterException e) {
            // Error in updating status
            Log.d("Twitter Update Error", e.getMessage());
        }
        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog and show
     * the data in UI Always use runOnUiThread(new Runnable()) to update UI
     * from background thread, otherwise you will get error
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(),
                        "Status tweeted successfully", Toast.LENGTH_SHORT)
                        .show();
                // Clearing EditText field
                txtUpdate.setText("");
            }
        });
    }

}

答案 3 :(得分:0)

你可以在twitPic4j API的帮助下完成这项工作。只需添加twitPic4j的API并在下面写下代码即可上传照片。

首先你必须从网址下载图片,保存到临时文件夹然后上传,上传后删除这个临时图片。

File picture = new File(APP_FILE_PATH + "/"+filename+".jpg");  
// Create TwitPic object and allocate TwitPicResponse object 
TwitPic tpRequest = new TwitPic(TWITTER_NAME, TWITTER_PASSWORD); 
TwitPicResponse tpResponse = null;  
// Make request and handle exceptions                            
try {         
        tpResponse = tpRequest.uploadAndPost(picture, customMessageEditText.getText()+" http://www.twsbi.com/");

} 
catch (IOException e) {         
        e.printStackTrace(); 
       Toast.makeText(getApplicationContext(), "Please enter valid username and password.", Toast.LENGTH_SHORT).show();
} 
catch (TwitPicException e) {         
        e.printStackTrace(); 
        Toast.makeText(getApplicationContext(), "Invalid username and password.", Toast.LENGTH_SHORT).show();
        Toast.makeText(getApplicationContext(), "Please enter valid Username and Password.", Toast.LENGTH_SHORT).show();
}  
// If we got a response back, print out response variables                               
if(tpResponse != null) {         
       tpResponse.dumpVars();
       System.out.println(tpResponse.getStatus());
       if(tpResponse.getStatus().equals("ok")){
            Toast.makeText(getApplicationContext(), "Photo posted on Twitter.",Toast.LENGTH_SHORT).show();
            //picture.delete();
       }
 }