Question

I'm developing an app that use twitter, and I need the user profile picture. In my MainActivity, I'm using NavigationDrawerFragment and the photo should be here, but I don't want the drawer before the user is logged in. For the login I'm using TwitterOAuthView. I'm also using AndroidUniversalImageLoader to load the profile photo. Thi is how I use the drawer only if the user is logged in, and initialize how I want to be configured the image loader:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mSharedPreferences = getSharedPreferences("MyPref", 0);
    connectionDetector = new ConnectionDetector(this);

    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .cacheOnDisc(true)
            .build();
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
            .defaultDisplayImageOptions(defaultOptions)
            .build();
    ImageLoader.getInstance().init(config);
}

@Override
public void onResume() {
    super.onResume();

    if (isTwitterLoggedInAlready()) {
        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

        Fragment fragment;
        FragmentManager fragmentManager;

        fragment = new MainFragment();

        fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, fragment).commit();
    }
    else {
        if(connectionDetector.isConnectingToInternet()){
            Fragment fragment;
            FragmentManager fragmentManager;

            fragment = new LoginFragment();

            fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.container, fragment).commit();
        }
        else {
            Toast.makeText(this, "internet connection required",
                    Toast.LENGTH_LONG).show();
        }
    }
}

After the login has been done, I get the profile picture URL from twitter in this way:

    private class GetProfilePictureURL extends AsyncTask<String,Integer,Void> {

    protected Void doInBackground(String... strings) {
        String access_token = strings[0];
        String access_token_secret = strings[1];

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

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

        try {
            User user = twitter.showUser(accessToken.getUserId());
            String urldisplay = user.getOriginalProfileImageURL();

            SharedPreferences mSharedPreferences = getSharedPreferences("MyPref", 0);

            // Shared Preferences
            SharedPreferences.Editor e = mSharedPreferences.edit();
            e.putString(TwitterKs.PREF_KEY_PICTURE_URL, urldisplay);
            e.commit();

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

        return null;
    }

    protected void onPostExecute(Long result) {
    }

}

So I save in SharedPreferences the URL of the photo. When the Activity in which the user log in is closed, in the first Activity onResume() is called and this time the drawer should be created.

In the drawer, to get the profile picture on onCreateView

ImageView pictureImage = (ImageView) insideDrawer.findViewById(R.id.
SharedPreferences mSharedPreferences = getActivity().getSharedPreferences("MyPref",
String urldisplay = mSharedPreferences.getString(TwitterKs.PREF_KEY_PICTURE_URL,"");
Bitmap bitmap = ImageLoader.getInstance().loadImageSync(urldisplay);
pictureImage.setImageBitmap(bitmap);

AND

Bitmap bitmap = ImageLoader.getInstance().loadImageSync(urldisplay);

gaves me NullPointerException when I've just started the application. But it's impossible cause I've never created the drawer yet! I'm a bit confused.... Any help? :(

Was it helpful?

Solution

Two adjustments:

  1. Your onCreateView will be called when the fragment is loaded. The code to update the image should be placed in the "onPostExecute" on your async method.

  2. It is recommended that you configure ImageLoader in an Application class that way you can call "getInstance" from any where within your app and it will be the same instance. I think in your case the instance created in onCreate no longer exist when trying to update later. Try adding an application class and try again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top