How to post image to twitter in android

2019-01-27 04:22发布

I want to post an image that is created in my app to twitter. I don't know how to do this and I was wondering if there is an SDK for titter like there is for Facebook? Thanks in advance

3条回答
Rolldiameter
2楼-- · 2019-01-27 04:53

You can use the Intent to post the image on Twitter and you can download the full source code from here download full source code

Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType(“image/jpeg”);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(getContentResolver(), b, “Title”, null);
Uri imageUri = Uri.parse(path);
share.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(share, “Select”));
查看更多
闹够了就滚
3楼-- · 2019-01-27 04:54

First you need to create an app on twitter

here is code to post message on twitter

 ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
        configurationBuilder.setOAuthConsumerKey(context.getResources().getString(R.string.twitter_consumer_key));
        configurationBuilder.setOAuthConsumerSecret(context.getResources().getString(R.string.twitter_consumer_secret));
        configurationBuilder.setOAuthAccessToken(LoginActivity.getAccessToken((context)));
        configurationBuilder.setOAuthAccessTokenSecret(LoginActivity.getAccessTokenSecret(context));
        Configuration configuration = configurationBuilder.build();
        final Twitter twitter = new TwitterFactory(configuration).getInstance();

        new Thread(new Runnable() {

                private double x;

                @Override
                public void run() {
                        boolean success = true;
                        try {
                                x = Math.random();
                                twitter.updateStatus(message +" "+x);
                        } catch (TwitterException e) {
                                e.printStackTrace();
                                success = false;
                        }

                        final boolean finalSuccess = success;

                        callingActivity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                        postResponse.onFinsihed(finalSuccess);
                                }
                        });

                }
        }).start(); 

check this tutorial for more details.

查看更多
ら.Afraid
4楼-- · 2019-01-27 05:12

I think you want to implement a sharing intent in Android.

This answer and code example by user "Second" looks applicable.

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");

share.putExtra(Intent.EXTRA_STREAM,
   Uri.parse("file:///sdcard/DCIM/Camera/myPic.jpg"));

startActivity(Intent.createChooser(share, "Share Image"));
查看更多
登录 后发表回答