2

可能重复:
我们可以使用 Android 中的 twitter API 在 twitter 上发布图像吗?
带有图像的 Android 推特推文

我必须从相机拍照并上传用户推文状态。我无能为力,请帮忙。我已使用以下代码发布文本但无法将位图中的照片上传到推特

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

3 回答 3

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();
            }
于 2012-11-30T05:06:28.437 回答
1

Twitter 将只更新状态而不是图片。如果您想实现,然后搜索将图像上传到 TwitPic,这将为您提供您在 TwitPic 上的图像的 bit.ly url。在 Twitter 上发布相同的 URL,这会将用户重定向到 Picture。

于 2012-11-30T04:51:28.030 回答
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();
        }
    }
于 2012-11-30T05:10:44.417 回答