0

I'm using Twitter4J library. I want to display the user picture profile in a circle. I'm new to using bitmaps and new to get something from a server.

Now, searching around, I'm using this way:

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];

        try {
            SharedPreferences mSharedPreferences = getActivity().getSharedPreferences("MyPref",0);

            ConfigurationBuilder builder = new ConfigurationBuilder();
            builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
            builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);

            // Access Token
            String access_token = mSharedPreferences.getString(
                    PREF_KEY_OAUTH_TOKEN, "");
            // Access Token Secret
            String access_token_secret = mSharedPreferences.getString(
                    PREF_KEY_OAUTH_SECRET, "");

            AccessToken accessToken = new AccessToken(access_token,
                    access_token_secret);
            Twitter twitter = new TwitterFactory(builder.build())
                    .getInstance(accessToken);

            User user = twitter.showUser(accessToken.getUserId());
            urldisplay = user.getProfileImageURL();




        } catch (TwitterException e) {
            // Error in updating status
            Log.d("Twitter Update Error", e.getMessage());
        }




        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

        final int width = mIcon11.getWidth();
        final int height = mIcon11.getHeight();
        final Bitmap outputBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        final Path path = new Path();
        path.addCircle(
                (float)(width / 2)
                , (float)(height / 2)
                , (float) Math.min(width, (height / 2))
                , Path.Direction.CCW);

        final Canvas canvas = new Canvas(outputBitmap);
        canvas.clipPath(path);
        canvas.drawBitmap(mIcon11, 0, 0, null);

        return outputBitmap;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

But in that way the picture is loaded every time I reopen the application..

I want to resize the bitmap and give it a border etc. but I'll search how to do this later. I think would be better if when the user login to twitter I download once his profile picture and then I reuse it every time is needed. Is it possible? What is the better way to do what I want? Thanks.

1 Answer 1

1

There are libraries you can you to cache the bitmaps. Have a look at these:

  1. Android-Universal-Image-Loader (Currently using this)
  2. Picasso (Just discovered this)

As for loading the images has a circle, went with this library:

1
  • Thank you, I've used Android-Universal-Image-Loader too :)
    – andrew
    Mar 3, 2014 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.