Build Android Wallpaper App with Fackbook Album

On July 25, 2015, in Android, by James Liu

In my last Android Wallpaper App series of tutorials, I have demonstrated how to build an Android Wallpaper App in 5 steps. After you finished all these tutorials, you will know how to use Android GridView Layout, download image by AsyncTask, save and load file locally, crop image and set it as wallpaper on Android. However, there is no tutorial covering how to build a server to support Android Wallpaper. Therefore, I improve my Android Wallpaper app to use Facebook as its wallpaper server.

The idea is from one of my friends. It will use Facebook album page as wallpaper server. You can create a Facebook album page with your Facebook account. Here is my Facebook album page url:

After we get the Facebook Album page ready, we can use Facebook Graphic API to load the photos from Facebook album by album id. The album is the first number fragment “1374811676174125” in the Facebook album page url. So we can use following Facebook Graphic API to retrieve the wallpapers from Facebook album:

To access the Facebook album resource, we need to use any valid access token if the album is public. Otherwise, we will get an OAuthException. If we directly use above url to access the album photos, we will get following error information:

“error”: {
“message”: “An access token is required to request this resource.”,
“type”: “OAuthException”,
“code”: 104

Get Client Token for Facebook Based Android App

To access the Facebook album photo resource through Facebook Graph API, an access token is required. Facebook has defined 4 types of access tokens, user access token, app access token, page access token, and client token. Basing on the Facebook document,

  • User Access Token is used to make all user relevant FaceBook Graph API Call, such as retrieve specific user information or share content on user timeline.
  • App Access Token is used to make all Facebook Graph API calls between app (server app, not native app) to facebook server. However, it is considered as not security to use App Access Token when your app is desktop app or native app. Therefore, the App Access Token is only recommended to be used between server to Facebook server. There are two way to get App Access Token. One is getting the token by Facebook Graph API request. Another one is using the string “app_id|app_secret”. For example, if your app id is 380892198237 and your app secret is c1e356f221468c7db835d25dc008724b, then your app access token is:


  • Page Tokens is more complicated. Page access tokens are used in Graph API calls to manage Facebook Pages. I will talk about this token in details when we use it in other tutorial.
  • Client Token is rarely used according with Facebook’s document. I also cannot find any other document to describe how to use it.

In this app, I will request a user access token by Facebook Graph API and use this token to get Facebook album photos. (Though we can use app access token to do so and it could be much easier, the Facebook recommends us not to use it in native app.)

Integrate Facebook Android SDK

Facebook provides a very detail help page to help us integrate the Facebook SDK in Android App. Here is the link:

Get Facebook User Access Token in Android App

As Facebook development document mentioned, we can only get the user access token by logging in the Facebook. In the Facebook example, it provides a button for user to click to popup a Facebook login panel. In our example, we want to popup a Facebook login panel without any user activities. So we directly popup a Facebook login programmatically. Here is the example source code:

protected void onCreate(Bundle savedInstanceState) {


protected void onResume() {

	AccessToken accessToken = AccessToken.getCurrentAccessToken();
	if(accessToken == null) {
		callbackManager = CallbackManager.Factory.create();
		LoginManager.getInstance().registerCallback(callbackManager, facebookCallBack);
		LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile"));

Facebook Login Error
After debug the app on my Galaxy Tablet, we successfully popup the Facebook login panel, but still get an error after key in the username and password. Here is the error message and the screenshot of the error.

The application has disabled OAuth client flow.


To solve this problem, please make sure that you have set the “Client OAuth Login” to “YES” in your Facebook App Setting page.
set facebook app Client OAuth Login

Then, run the app again and we can login the Facebook successfully via Facebook SDK.

Get Facebook Photos from Album Page by Facebook Graph API

After we successfully get the user access token from Facebook API, we can call following Facebook Graph API to get the photos from the album.

The above Facebook Graph API will return a JSON formatted data, which only contain one image.

   "data": [
         "id": "1377718815883411",
         "created_time": "2015-03-10T09:30:55+0000",
         "from": {
            "name": "WallPaper For FackBook Page Albums Photos",
            "category": "App Page",
            "id": "1374807089507917"
         "height": 720,
         "icon": "",
         "images": [
               "height": 960,
               "source": "",
               "width": 720
               "height": 800,
               "source": "",
               "width": 600
               "height": 640,
               "source": "",
               "width": 480
               "height": 426,
               "source": "",
               "width": 320
               "height": 540,
               "source": "",
               "width": 405
               "height": 173,
               "source": "",
               "width": 130
               "height": 225,
               "source": "",
               "width": 168
         "link": "",
         "picture": "",
         "source": "",
         "updated_time": "2015-03-10T09:30:56+0000",
         "width": 540
   "paging": {
      "cursors": {
         "before": "MTM3NzcxODgxNTg4MzQxMQZDZD",
         "after": "MTM3NzcxODg1NTg4MzQwNwZDZD"

Decode JSON Formatted Data and Save Photo Data

Here is an example source code to decode the above json code. This source code will loop the data and get the image url for Wallpaper App.

url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//connection.setReadTimeout(10 * 1000);
//connection.setConnectTimeout(10 * 1000);
int response = connection.getResponseCode();
is = connection.getInputStream();
String jsonStr = Utils.readStringFromStream(is);
JSONObject jsonObject = new JSONObject(jsonStr);
JSONArray photoObjArray = jsonObject.getJSONArray("data");
int numberOfPhotos = photoObjArray.length();
for(int i=0; i<numberOfPhotos; i++) {
	JSONObject photoObj = photoObjArray.getJSONObject(i);
	PhotoData photoData = new PhotoData(); = photoObj.getString("id");
	photoData.createDate = photoObj.getString("created_time");

	//smallest size for thumbnial
	JSONArray sampleArray = photoObj.getJSONArray("images");
	int numberOfSample = sampleArray.length();
	JSONObject sampleObj = sampleArray.getJSONObject(numberOfSample - 1);
	photoData.thumbnailURL = sampleObj.getString("source");

	//biggest size for preview
	sampleObj = sampleArray.getJSONObject(0);
	photoData.sourceURL = sampleObj.getString("source");
	photoData.height = sampleObj.getString("height");
	photoData.width = sampleObj.getString("width");

Let’s run the app and see how it looks like.
wallpaper app

Try Example Android App

You can download and install this android app by click the following link, or scan the following QR code to download and install on your android phone simply. If you feel it is helpful, please support me by clicking the banner inside the app.
Download and Run This Example on Your Android Phone

Get Full Source Code under $19.99

You will get whole Android Wallpaper App source code at $19.99. With this source code, you can do everything you want:

  • Load your own wallpaper from Facebook Album;
  • Monetize the app with your own AdMob publish id;
  • Use the source code in your own project;
  • Publish this app in your android develop account;

How to customize the app to use with your Facebook albums?

In this Android Wallpaper app, you can load the images from several Facebook albums. Once you get the app source code, you can replace with your own album ids. To use your own album ids, please follow these steps:

  • Make sure your Facebook Album privacy is public;
  • Find and edit, add your Facebook album id in the albumIDs array
  • //the following example albumIDs has two album ids. If you have only one Facebook album, write like this: private String[] albumIDs = {"1374811676174125"};
    private String[] albumIDs = {"1374811676174125", "1377718805883412"}; //replace the album id number with yours

2 Responses to “Build Android Wallpaper App with Fackbook Album”

  1. pc games says:

    I really llve your site.. Very nice colors & theme.

    Did you make this sitte yourself? Please reply back as I’m looking to
    create my own blog and want to know where you got this from or just what the theme
    is named. Thanks!

Leave a Reply

Free WordPress Themes

Free WordPress Theme