如何在android中的用户twitter个人资料中上传照片

时间:2012-11-30 04:43:51

标签: android twitter upload photo

  

可能重复:
  Can we post image on twitter using twitter API in Android?
  Android twitter tweet with image

我必须从相机拍照并上传用户推文状态。我无法请求帮助。我使用了followig代码来发布文本,但无法将位图中的照片上传到twiiter

 public void shareTwitter()
         {
             try {
                String token =  myPrefs.getString(FindFriends.PREF_KEY_OAUTH_TOKEN, "");
                String secret =  myPrefs.getString(FindFriends.PREF_KEY_OAUTH_SECRET, "");

                    ConfigurationBuilder cb = new ConfigurationBuilder();
                    cb.setDebugEnabled(true)
                    .setOAuthConsumerKey(FindFriends.TWITTER_CONSUMER_KEY)
                    .setOAuthConsumerSecret(FindFriends.TWITTER_CONSUMER_SECRET)
                    .setOAuthAccessToken(token)
                    .setOAuthAccessTokenSecret(secret);
                    AccessToken accessToken = new AccessToken(token, secret);
                    Twitter twitter = new TwitterFactory(cb.build()).getInstance(accessToken);
                    twitter.updateStatus("hello");


             } catch (Exception e) {
                e.printStackTrace();

3 个答案:

答案 0 :(得分:6)

试试这段代码,希望这会是你。

 Twitter twitter = new TwitterFactory(conf).getInstance();
            Bitmap bmp = BitmapFactory.decodeResource(
                    TwitterFriends.this.getResources(), R.drawable.edit_ic);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();
            ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
            StatusUpdate status = new StatusUpdate(message);
            status.setMedia("newyear", bis);

            try {
                twitter.updateStatus(status);
            } catch (Exception e) {
                e.printStackTrace();
            }

答案 1 :(得分:1)

Twitter将更新状态而不是图片。如果你想实现然后搜索上传图像到TwitPic,这将在TwitPic上给你一个你的图像的网址。在Twitter上发布相同的网址,将用户重定向到图片。

答案 2 :(得分:0)

这是上传按钮......

        upload.setOnClickListener(new OnClickListener()
        {   
            @Override
            public void onClick(View v) 
            {
                new ImageSender().execute();

            }
        });

这是异步任务......

private class ImageSender extends AsyncTask<URL, Integer, Long> 
    {
        private String url;

        protected void onPreExecute() 
        {
            //mProgressDialog = ProgressDialog.show(SendImageActivity.this, "", "Sending image...", true);

            //mProgressDialog.setCancelable(false);
            //mProgressDialog.show();
        }

        protected Long doInBackground(URL... urls) 
        {            
            long result = 0;
            prefs = PreferenceManager.getDefaultSharedPreferences(TestingTwitterActivity.this);
            String token1=prefs.getString("token", null);
            String tokenSecret1=prefs.getString("tokenSecret", null);
            Configuration conf = new ConfigurationBuilder()                 
            .setOAuthConsumerKey(twitter_consumer_key) 
            .setOAuthConsumerSecret(twitter_secret_key) 
            .setOAuthAccessToken(token1) 
            .setOAuthAccessTokenSecret(tokenSecret1) 
            .build(); 

            OAuthAuthorization auth = new OAuthAuthorization (conf, conf.getOAuthConsumerKey (), conf.getOAuthConsumerSecret (),
                    new AccessToken (conf.getOAuthAccessToken (), conf.getOAuthAccessTokenSecret ()));

            ImageUpload upload = ImageUpload.getTwitpicUploader (twitpic_api_key, auth);

            //Log.d(TAG, "Start sending image...");

            try {
                url = upload.upload(new File(mPath));//here your camera pic file path...
                result = 1;

                //Log.d(TAG, "Image uploaded, Twitpic url is " + url);          
            } catch (Exception e) {        
                //Log.e(TAG, "Failed to send image");

                e.printStackTrace();
            }

            return result;
        }

        protected void onProgressUpdate(Integer... progress) 
        {            
        }

        protected void onPostExecute(Long result) 
        {
            //mProgressDialog.cancel();

            String text = (result == 1) ? "Image sent successfully.\n Twitpic url is: " + url : "Failed to send image";

            Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
        }
    }