从Android应用程序发布带有文本的图像到linkedIn

时间:2014-04-10 09:56:48

标签: java android linkedin

我想将带有文字的SD卡图片发布到我的Android应用程序中的linkedIn。

我可以分享文字,但图片不会分享。

我尝试使用以下代码:

share.setOnClickListener(new OnClickListener() {
    @Override

     public void onClick(View v) {

            String share = et.getText().toString();
            if (null != share && !share.equalsIgnoreCase("")) {

                OAuthConsumer consumer = new CommonsHttpOAuthConsumer(Config.LINKEDIN_CONSUMER_KEY, Config.LINKEDIN_CONSUMER_SECRET);
                consumer.setTokenWithSecret(accessToken.getToken(), accessToken.getTokenSecret());

                DefaultHttpClient httpclient = new DefaultHttpClient();
                HttpPost post = new HttpPost("https://api.linkedin.com/v1/people/~/shares");
                try {
                    consumer.sign(post);
                } catch (OAuthMessageSignerException e) {
                    e.printStackTrace();
                } catch (OAuthExpectationFailedException e) {
                    e.printStackTrace();
                } catch (OAuthCommunicationException e) {
                    e.printStackTrace();
                } // here need the consumer for sign in for post the share
                post.setHeader("content-type", "text/XML");
                byte[] data = null;
             try {
            ileInputStream fis = new FileInputStream(imgUrl1);
            Bitmap bi = BitmapFactory.decodeStream(fis);
            ByteArrayOutputStream baos = new ByteArrayOutputStream()
                   bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            data = baos.toByteArray();  
            }
              catch (FileNotFoundException e) 
               { 
             e.printStackTrace();
         Log.d("onCreate", "debug error  e = " + e.toString());
           }     

         String myEntity = "<share><comment>"+  text +"</comment>    <content><submitted-image-url>data</submitted-image-url></content><visibility><code>anyone</code></visibility></share>";

              try {
                    post.setEntity(new StringEntity(myEntity));
                    org.apache.http.HttpResponse response = httpclient.execute(post);
                    Toast.makeText(LinkedInSampleActivity.this,
                            "Shared sucessfully", Toast.LENGTH_SHORT).show();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else {
                Toast.makeText(LinkedInSampleActivity.this,
                        "Please enter the text to share",
                        Toast.LENGTH_SHORT).show();
            }
        }
    });
}

1 个答案:

答案 0 :(得分:1)

在linkedIn中,您无法共享本地图像。 为此,您需要将图像存储在服务器中并获取该图像的URL,现在您可以将图像发布到linkedIn ...

这可能对你有所帮助......