0

嗨,我在这里做了一个应用程序,我需要在推特上分享我的分数,我确实使用下面的代码,我的分数很好,但现在我需要与应用程序图标一起分享分数。但我不知道如何与文本一起分享该图像谁能帮帮我,谢谢

    TestPost.class: 
public class TestPost extends Activity {
String review;
private TwitterApp mTwitter;

private String username = "";
private boolean postToTwitter = false;

private static final String twitter_consumer_key = "";
private static final String twitter_secret_key = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.post);

    Button postBtn              = (Button) findViewById(R.id.button1);
    final EditText reviewEdit   = (EditText) findViewById(R.id.revieew);

    postBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
             review = reviewEdit.getText().toString();
            postToTwitter = true;



            onTwitterClick();

        }
    });

    mTwitter = new TwitterApp(this, twitter_consumer_key,twitter_secret_key);

    mTwitter.setListener(mTwLoginDialogListener);



    if (mTwitter.hasAccessToken()) {
        username    = mTwitter.getUsername();
        username    = (username.equals("")) ? "No Name" : username;


    }
}



private void postToTwitter(final String review) {
    new Thread() {
        @Override
        public void run() {
            int what = 0;

            try {
                mTwitter.updateStatus(review);
            } catch (Exception e) {
                what = 1;
            }

            mHandler.sendMessage(mHandler.obtainMessage(what));
        }
    }.start();
}

private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        String text = (msg.what == 0) ? "Posted to Twitter" : "Post to Twitter failed";

        Toast.makeText(TestPost.this, text, Toast.LENGTH_SHORT).show();
    }
};
private final TwDialogListener mTwLoginDialogListener = new TwDialogListener() {
    @Override
    public void onComplete(String value) {
        username    = mTwitter.getUsername();
        username    = (username.equals("")) ? "No Name" : username;




        postToTwitter = true;
        postToTwitter(review);

        Toast.makeText(TestPost.this, "Connected to Twitter as " + username, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onError(String value) {


        Toast.makeText(TestPost.this, "Twitter connection failed", Toast.LENGTH_LONG).show();
    }
};
private void onTwitterClick() {
    if (mTwitter.hasAccessToken()) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setMessage("Delete current Twitter connection?")
               .setCancelable(false)
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       mTwitter.resetAccessToken();


                   }
               })
               .setNegativeButton("No", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();


                   }
               });
        final AlertDialog alert = builder.create();

        alert.show();
    } else {


        mTwitter.authorize();
    }
}

   }
4

1 回答 1

0

这就是我将图像和文本上传到登录的 Twitter 用户帐户的方式。为此,我使用了TwitPic API。您必须在那里登录并注册一个开发者帐户。

该解决方案可以被认为是一个两步解决方案。

首先,当用户点击发布按钮时,我首先抓取图片并上传到 TwitPic。从那里,我获取了 TwitPic ( String url = upload.upload(finalFile);) 返回的 URL。

在此代码的第二步中,在一个 String 实例 ( String finalStatusWithURL) 中,我获取了 an 的内容,EditText然后将步骤 1 中的 URL 附加到它上面。完成此操作后,该帖子最终发布到 Twitter。

Configuration conf = new ConfigurationBuilder()
.setOAuthConsumerKey(TWITTER_CONSUMER_KEY)
.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET)
.setOAuthAccessToken(twit_access_token)
.setOAuthAccessTokenSecret(twit_access_token_secret)
.setMediaProviderAPIKey(TWIT_PIC_API)
.build();

// SET THE FILE PATH
File finalFile = new File(getRealPathFromURI(initialURI));
// THIS IS IMPORTANT. TWITPIC NEEDS THE ACTUAL PATH ON THE DEVICE. JUST THE URI DOES NOT WORK!!!!

ImageUpload upload = new ImageUploadFactory(conf).getInstance(MediaProvider.TWITPIC);

String url = upload.upload(finalFile);
Log.e("TWITTER URL RESPONSE", url);

// NOW, UPLOAD TO TWITTER

ConfigurationBuilder builder = new ConfigurationBuilder();

builder.setOAuthConsumerKey(YOUR_TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(YOUR_TWITTER_CONSUMER_SECRET);

AccessToken accessToken = new AccessToken(your_twit_access_token, your_twit_access_token_secret);
Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);

String finalStatusWithURL = null;

if (finalStatusMessage.trim().length() > 0) {

    finalStatusMessage = editStatusUpdate.getText().toString();
    finalStatusWithURL = finalStatusMessage + ":\n " + url;

} else {
    finalStatusWithURL = url;
}

twitter4j.Status response = twitter.updateStatus(finalStatusWithURL);
Log.e("TWITTER RESPONSE", response.getText());

这是获取您要上传的图像的真实路径的方法:

// HELPER METHOD TO GET REAL PATH FOR THE SELECTED IMAGE
public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null); 
    cursor.moveToFirst(); 
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
    return cursor.getString(idx); 
}

希望这可以帮助。这是我的应用程序的生产代码。

更新:

在代码中更改它:

File finalFile = new File(getRealPathFromURI(initialURI));

对此:

Uri tempUri = getImageUri(getApplicationContext(), icon);
File finalFile = new File(getRealPathFromURI(tempUri));

辅助方法Uri tempUri = getImageUri(getApplicationContext(), bmpFinal);

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

您仍然需要传递一个 Bitmap 对象。要将您的应用程序图标转换为位图,请使用以下命令:

Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);

更新2:

在您提出问题并我回答您的问题后不久,Twitter 结束了在 SF 的开发人员会议。他们介绍了一些我认为可能对您的特定用例有用的新卡片。它是新鲜的,我还没有开始测试/使用它。但如果它符合您的目的,请查看以下链接:

  1. http://www.engadget.com/2013/04/02/twitter-cards-apps-products-photo-galleries/(我在哪里发现了新功能)
  2. https://dev.twitter.com/blog/mobile-app-deep-linking-and-new-cards(官方推特博客)
  3. https://dev.twitter.com/cards(开发者页面了解功能的详细信息以及如何使用它们)
于 2013-04-02T10:07:09.753 回答