简体   繁体   中英

Android - Manipulate Twitter Profile Picture

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.

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM