Integrating Google photo to Android application

Overview

Picasa – Google photo is a fast and easy sharing photo from Google. We can access Google photo album by integrating Android application.

Our steps:

  • Ask user to pick a google account
  • When we get the account back, ask for access to the picasa picture
  • Then we get our token
  • Get albums and images from picasa by our token

How to integrating

  1. Adding libraries by gradle
    compile 'com.google.android.gms:play-services:9.0.1'
    compile group: 'com.google.gdata', name: 'core', version: '1.47.1'
    
  2. Ask user to pick a google account
    Intent googlePicker = AccountPicker.newChooseAccountIntent(null, null, new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, true, null, null, null, null);
    super.startActivityForResult(googlePicker, Config.PICK_PICASA_ALBUM);
    
  3. Get token
    @Override
    public void onActivityResult(int requestCode, int result, Intent data) {
        if (result == RESULT_OK && requestCode == Config.PICK_PICASA_ALBUM) {
            super.onActivityResult(requestCode, result, data);
            this.email = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
            this.type = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
            this.getTokenOfPicasa();
        }
    }
    
    private void getTokenOfPicasa() {
        Account account = new Account(this.email, this.type);
        AccountManager accountManager = AccountManager.get(this);
        accountManager.getAuthToken(account, "lh2", null, this,
            new AccountManagerCallback<Bundle>() {
                @Override
                public void run(AccountManagerFuture<Bundle> result) {
                    try {
                        Bundle bundle = result.getResult();
                        if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
                            authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                            new GetAlbumsOfPicasa().execute();
                        }
                    } catch (Exception e) {
                            Log.e(TAG, "getTokenOfPicasa", e);
                    }
                }
            }
        }, null);
    }
    
  4. Get albums and images
    private class GetAlbumsOfPicasa extends AsyncTask<Void, Void, Void> {
        private List<Album> albums;
    
        @Override
        protected Void doInBackground(Void... params) {
            // Get albums and images of Picasa
            getAlbumsOfPicasa(email, authToken);
            return null;
        }
    
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
        }
    }
    
    public List<AlbumEntry> getAlbumsOfPicasa(String email, String authToken) {
        try {
            String API_PREFIX = "https://picasaweb.google.com/data/feed/api/user/";
    
            PicasawebService picasaService = new PicasawebService("pictureframe");
            picasaService.setUserToken(authToken);
    
            String albumUrl = API_PREFIX + email;
            UserFeed userFeed = picasaService.getFeed(new URL(albumUrl), UserFeed.class);
    
            List<GphotoEntry> entries = userFeed.getEntries();
            List<AlbumEntry> albums = new ArrayList<>();
            for (GphotoEntry entry : entries) {
                AlbumEntry albumEntry = new AlbumEntry(entry);
                albums.add(albumEntry);
            }
            return albums;
        } catch (Exception e) {
            Log.e(TAG, "getAlbumsOfPicasa", e);
            return null;
        }
    }
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s