Skip to main content

Featured

Create a chat application using openfire server and smack library

Targeted audience for this blog are developers having knowledge of core java, android, XMPP Protocol, socket programming and FTP protocol. Following are the steps to build the chat application using openfire server and smack library : 1.) Install openfire server and configure on pc  :- You can follow :    https://docs.bmc.com/docs/itsm1805/installing-and-configuring-the-openfire-chat-server-804709699.html   to refer how to install the server on your PC.   The typical openfire server admin console looks like below :  You can download and install lots of available plugins and extend the functionality of the server.      Below is the source from where you can get the openfire plugins.       https://www.igniterealtime.org/projects/openfire/plugins.jsp 2.) Add the smack library into your android project, below are the source for integrating the latest smack library for android : https://github.com/igniterealtime/S...

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