Tweet from android (Twitter fabric integration)

TweetComposerActivity (Tweeting text and image that is retreived from Internet)



package com.example.tweet;

import io.fabric.sdk.android.Fabric;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.crashlytics.android.Crashlytics;
import com.twitter.sdk.android.Twitter;
import com.twitter.sdk.android.core.TwitterAuthConfig;
import com.twitter.sdk.android.tweetcomposer.TweetComposer;

public class TweetComposerActivity extends ActionBarActivity {

    private ImageView iv_tweet;
    private static final String TWITTER_KEY = "________"; //Add your key
    private static final String TWITTER_SECRET = "------------------"; //Add your secret
    private Uri.Builder builder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY,
            TWITTER_SECRET);
        setContentView(R.layout.activity_tweet_composer);

        Fabric.with(this, new TweetComposer(), new Twitter(authConfig),
            new Crashlytics());

        iv_tweet = (ImageView) findViewById(R.id.iv_tweet);

        iv_tweet.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                new postTweet().execute();

            }

        });

        // File myImageFile = new File(
        // "android.resource://example.com.testapplication/"
        // + R.drawable.ic_launcher);

        // Uri myImageUri = Uri.fromFile(myImageFile);

    }

    public class postTweet extends AsyncTask < Void, Void, Void > {

        ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            pDialog = new ProgressDialog(TweetComposerActivity.this);

            pDialog.setMessage("Setting up Tweet!!!");
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void...params) {
            // TODO Auto-generated method stub

            String Text = getResources().getString(R.string.news_title);

            try {

                Bitmap x;

                String url = "http://i.space.com/images/i/000/046/603/original/atmercury_lg.jpg?1427418303";

                HttpURLConnection connection = (HttpURLConnection) new URL(url)
                    .openConnection();
                connection.connect();
                InputStream input = connection.getInputStream();

                x = BitmapFactory.decodeStream(input);
                BitmapDrawable image = new BitmapDrawable(x);

                OutputStream outStream = null;

                String extStorageDirectory = Environment
                    .getExternalStorageDirectory().toString();

                File file = new File(extStorageDirectory, "image.PNG");
                try {
                    outStream = new FileOutputStream(file);
                    x.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                    outStream.flush();
                    outStream.close();

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();

                }

                // Uri imageUri = Uri
                // .parse(ContentResolver.SCHEME_ANDROID_RESOURCE
                // + "://"
                // + getResources().getResourcePackageName(
                // R.drawable.image)
                // + '/'
                // + getResources().getResourceTypeName(
                // R.drawable.image)
                // + '/'
                // + getResources().getResourceEntryName(
                // R.drawable.image));
                // Uri imageUri = getUriFromUrl(thisUrl);

                Uri ImageUri = Uri.fromFile(file);

                TweetComposer.Builder builder = new TweetComposer.Builder(
                    TweetComposerActivity.this).text(Text).image(ImageUri);

                builder.show();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            if (pDialog.isShowing()) {
                pDialog.dismiss();
            }
        }

    }

    public Uri getUriFromUrl(String thisUrl) {

        try {
            URL url;

            url = new URL(thisUrl);
            builder = new Uri.Builder().scheme(url.getProtocol())
                .authority(url.getAuthority()).appendPath(url.getPath());
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return builder.build();
    };

}


 

Comments

Most viewed