AssetLibrary

View as Markdown

AssetLibrary

An asset is a file, for example an image, video, PDF or audio file, that a stack stores in its repository. The AssetLibrary class fetches a list of published assets. Use this class to

  • list the assets published to an environment
  • filter the list on one field value and sort it
  • get the total number of assets
  • add metadata or fallback-locale details to each asset

stack.assetLibrary() returns the object and takes no arguments. See the assetLibrary method. Each asset in the result is an Asset object.

fetchAll

fetchAll retrieves the list of published assets in the stack, with the filters and flags you chained.

NameTypeDescription
assetsCallback (required)FetchAssetsCallback

Receives the asset list when the request ends.

Default: Not applicable

Returns: void. The result arrives in the callback.

Validation

  • There is no client-side validation.
  • fetchAll does not throw when the API rejects the request or the network fails.
    • onCompletion receives a non-null Error and a null asset list instead.
    • Check error inside the callback before you read the list, even when the call sits inside a try block.
  • With a null callback, fetchAll sends no network request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • fetchAll sends GET /assets, the Get all assets request.
  • The request carries every query parameter that where, sort, includeCount, includeFallback, includeMetadata and includeRelativeUrl stored, plus the stack's environment as the environment query parameter.
  • onCompletion(ResponseType responseType, List<Asset> assets, Error error) fires when the request ends.
    • On success error is null, and assets holds one filled Asset per asset in the response. The list is empty when no asset matches.
    • On failure error holds the status, code and message, and assets is null.
  • The API returns the first 100 assets. AssetLibrary has no skip or limit method. See Pagination on the AssetLibrary class page.
  • When the response carries a count, fetchAll stores it for the getCount method.
  • The cache policy decides whether fetchAll sends a request. The default IGNORE_CACHE always sends one. See the setCachePolicy method.

Example

Basic usage: list the published assets

import android.util.Log;

import com.contentstack.sdk.Asset;
import com.contentstack.sdk.AssetLibrary;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.FetchAssetsCallback;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.List;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    AssetLibrary assetLibrary = stack.assetLibrary();
    assetLibrary.fetchAll(new FetchAssetsCallback() {
        @Override
        public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
            if (error == null) {
                for (Asset asset : assets) {
                    Log.d("Contentstack", asset.getFileName() + " at " + asset.getUrl());
                }
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

Filtered list: PNG assets, newest first, with the total count

import android.util.Log;

import com.contentstack.sdk.Asset;
import com.contentstack.sdk.AssetLibrary;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.FetchAssetsCallback;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.List;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    AssetLibrary assetLibrary = stack.assetLibrary();
    assetLibrary.where("content_type", "image/png")
            .sort("created_at", AssetLibrary.ORDERBY.DESCENDING)
            .includeCount();
    assetLibrary.fetchAll(new FetchAssetsCallback() {
        @Override
        public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
            if (error != null) {
                Log.e("Contentstack", error.getErrorMessage());
                return;
            }
            Log.d("Contentstack", "Total: " + assetLibrary.getCount() + ", returned: " + assets.size());
        }
    });
} catch (Exception e) {
    Log.e("Contentstack", e.getMessage());
}

getCount

getCount returns the asset count that the last fetchAll response carried.

Returns: int. The asset count from the last fetchAll response.

Validation

  • There is no client-side validation. getCount reads a stored value and makes no request.

Behavior

  • getCount returns 0 until a fetchAll call completes with a count.
  • For a network result, it returns the count value of the response. The API adds count only after you call includeCount.
  • Without includeCount, a network result leaves the value unchanged, so getCount returns 0 or the count from an earlier call.
  • For a result from the SDK's cache, getCount returns the number of assets in the cached list, not the total in the stack.
  • Read getCount inside the onCompletion callback of fetchAll.

Example

Basic usage: read the total asset count

import android.util.Log;

import com.contentstack.sdk.Asset;
import com.contentstack.sdk.AssetLibrary;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.FetchAssetsCallback;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.List;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    AssetLibrary assetLibrary = stack.assetLibrary();
    assetLibrary.includeCount();
    assetLibrary.fetchAll(new FetchAssetsCallback() {
        @Override
        public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Total assets: " + assetLibrary.getCount());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

includeCount

includeCount asks the API to add the total number of matching assets to the response.

Returns: AssetLibrary. The same asset library, for chaining.

Validation

  • There is no client-side validation.
  • An invalid value surfaces as an Error in the callback when fetchAll sends the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. includeCount stores include_count=true, and fetchAll sends it.
  • Per the API's Count request, which works for assets as well as entries, the response then carries the total count with the asset details.
  • Read the total with the getCount method inside the fetchAll callback.
  • Compare the total with the size of the list to learn whether the stack holds more assets than the first 100 that the API returns.

Example

Basic usage: compare the total with the returned assets

import android.util.Log;

import com.contentstack.sdk.Asset;
import com.contentstack.sdk.AssetLibrary;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.FetchAssetsCallback;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.List;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    AssetLibrary assetLibrary = stack.assetLibrary();
    assetLibrary.includeCount();
    assetLibrary.fetchAll(new FetchAssetsCallback() {
        @Override
        public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Total: " + assetLibrary.getCount() + ", returned: " + assets.size());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

includeFallback

includeFallback asks the API to return the fallback-locale details of an asset when the requested locale has no published version.

Returns: AssetLibrary. The same asset library, for chaining.

Validation

  • There is no client-side validation.
  • An invalid value surfaces as an Error in the callback when fetchAll sends the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. includeFallback stores include_fallback=true, and fetchAll sends it.
  • Per the Get all assets request, the API then includes the published asset details from the fallback locale when the specified locale holds no published content.
  • AssetLibrary has no method that sets a locale, so fetchAll sends no locale parameter.

Example

Basic usage: list assets with fallback details

import android.util.Log;

import com.contentstack.sdk.Asset;
import com.contentstack.sdk.AssetLibrary;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.FetchAssetsCallback;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.List;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    AssetLibrary assetLibrary = stack.assetLibrary();
    assetLibrary.includeFallback();
    assetLibrary.fetchAll(new FetchAssetsCallback() {
        @Override
        public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
            if (error == null) {
                for (Asset asset : assets) {
                    Log.d("Contentstack", asset.getFileName() + " at " + asset.getUrl());
                }
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

includeMetadata

includeMetadata asks the API to add the metadata of each asset to the response.

Returns: AssetLibrary. The same asset library, for chaining.

Validation

  • There is no client-side validation.
  • An invalid value surfaces as an Error in the callback when fetchAll sends the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. includeMetadata stores include_metadata=true, and fetchAll sends it.
  • Per the Get all assets request, the API returns the metadata of each asset under its _metadata key.
  • Asset has no getter for _metadata. Read it from the JSON that the toJSON method returns.

Example

Basic usage: read the metadata of each asset

import android.util.Log;

import com.contentstack.sdk.Asset;
import com.contentstack.sdk.AssetLibrary;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.FetchAssetsCallback;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.List;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    AssetLibrary assetLibrary = stack.assetLibrary();
    assetLibrary.includeMetadata();
    assetLibrary.fetchAll(new FetchAssetsCallback() {
        @Override
        public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
            if (error == null) {
                for (Asset asset : assets) {
                    Log.d("Contentstack", asset.getFileName() + ": " + asset.toJSON().optJSONObject("_metadata"));
                }
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

includeRelativeUrl

includeRelativeUrl adds the relative_urls flag to the request, which asks for relative asset URLs in the response.

Returns: AssetLibrary. The same asset library, for chaining.

Validation

  • There is no client-side validation.
  • An invalid value surfaces as an Error in the callback when fetchAll sends the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. includeRelativeUrl stores relative_urls=true, and fetchAll sends it.
  • Read each URL with the getUrl method on the returned assets.

Example

Basic usage: list asset URLs

import android.util.Log;

import com.contentstack.sdk.Asset;
import com.contentstack.sdk.AssetLibrary;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.FetchAssetsCallback;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.List;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    AssetLibrary assetLibrary = stack.assetLibrary();
    assetLibrary.includeRelativeUrl();
    assetLibrary.fetchAll(new FetchAssetsCallback() {
        @Override
        public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
            if (error == null) {
                for (Asset asset : assets) {
                    Log.d("Contentstack", "URL: " + asset.getUrl());
                }
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

removeHeader

removeHeader removes one request header that the setHeader method set on this library.

NameTypeDescription
key (required)String

Name of the header to remove.

Default: Not applicable

Returns: void. This method returns nothing.

Validation

  • There is no client-side validation of the header name.
  • removeHeader ignores a null or empty key, and a key that the library does not hold.

Behavior

  • Client-side only. removeHeader changes the headers that fetchAll sends for this library.
  • removeHeader removes only headers set on the library. It cannot remove a stack header such as environment or access_token.
  • Use the removeHeader method on Stack to remove a stack header.

Example

Basic usage: drop a header before the request

import android.util.Log;

import com.contentstack.sdk.Asset;
import com.contentstack.sdk.AssetLibrary;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.FetchAssetsCallback;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.List;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    AssetLibrary assetLibrary = stack.assetLibrary();
    assetLibrary.setHeader("branch", "<BRANCH>");
    assetLibrary.removeHeader("branch");
    assetLibrary.fetchAll(new FetchAssetsCallback() {
        @Override
        public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
            if (error == null) {
                for (Asset asset : assets) {
                    Log.d("Contentstack", asset.getFileName() + " at " + asset.getUrl());
                }
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

setCachePolicy

setCachePolicy sets where fetchAll reads the asset list from, for every AssetLibrary in the app.

NameTypeDescription
cachePolicy (required)CachePolicy

Where fetchAll reads the asset list from.

Default: Not applicable

Returns: void. This method returns nothing.

Validation

  • There is no client-side validation.
  • A null policy makes every later fetchAll send no request and never call onCompletion. Pass a CachePolicy value to restore requests.

Behavior

  • The default policy is IGNORE_CACHE.
  • Every AssetLibrary in the app shares one cache policy. A call on one library changes the policy for every library, including libraries created earlier, until the next setCachePolicy call.
  • The table below lists what fetchAll does for each CachePolicy value:
ValueWhat fetchAll does
IGNORE_CACHESends the request. This is the default.
NETWORK_ONLYSends the request.
CACHE_ONLYReads the cache and sends no request. onCompletion receives an Error when no cached result less than 60 seconds old exists.
CACHE_ELSE_NETWORKReads the cache when the cached result is less than 60 seconds old, and sends the request otherwise.
CACHE_THEN_NETWORKDelivers the cached result when one exists, then sends the request. onCompletion then fires twice, first with CACHE and then with NETWORK.
NETWORK_ELSE_CACHESends the request when the device has a connection, and reads the cache as CACHE_ONLY does otherwise.
  • IGNORE_CACHE and NETWORK_ONLY behave the same. Both send the request and save the response in the SDK's cache.
  • responseType in onCompletion names the source of the result: NETWORK, CACHE or UNKNOWN.
  • For a cached result, getCount returns the number of assets in the cached list.

Known issue The cache keeps one result for all asset-list requests on a stack, whatever filters, sort or flags they carry. With a policy that reads the cache, fetchAll can return the assets of an earlier, different request. Use IGNORE_CACHE or NETWORK_ONLY when the app runs more than one asset-list request.

Example

Basic usage: read the cached list when it is fresh

import android.util.Log;

import com.contentstack.sdk.Asset;
import com.contentstack.sdk.AssetLibrary;
import com.contentstack.sdk.CachePolicy;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.FetchAssetsCallback;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.List;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    AssetLibrary assetLibrary = stack.assetLibrary();
    // Applies to every AssetLibrary in the app
    assetLibrary.setCachePolicy(CachePolicy.CACHE_ELSE_NETWORK);
    assetLibrary.fetchAll(new FetchAssetsCallback() {
        @Override
        public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Source: " + responseType + ", assets: " + assets.size());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

setHeader

setHeader adds one request header that fetchAll sends for this library only.

NameTypeDescription
key (required)String

Name of the header.

Default: Not applicable
value (required)String

Value of the header.

Default: Not applicable

Returns: void. This method returns nothing.

Validation

  • There is no client-side validation of the header name or value.
  • setHeader ignores the call when key or value is null or empty.

Behavior

  • Client-side only. setHeader stores the header on this library, and fetchAll sends it.
  • A header set on the library overrides a stack header with the same name, for this library only.
  • A second call with the same key replaces the value.
  • Use the setHeader method on Stack to send a header with every request.

Example

Basic usage: list the assets of another branch

import android.util.Log;

import com.contentstack.sdk.Asset;
import com.contentstack.sdk.AssetLibrary;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.FetchAssetsCallback;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.List;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    AssetLibrary assetLibrary = stack.assetLibrary();
    // Overrides the stack's branch header for this library only
    assetLibrary.setHeader("branch", "<BRANCH>");
    assetLibrary.fetchAll(new FetchAssetsCallback() {
        @Override
        public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
            if (error == null) {
                for (Asset asset : assets) {
                    Log.d("Contentstack", asset.getFileName() + " at " + asset.getUrl());
                }
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

sort

sort orders the assets that fetchAll returns by the value of one field.

NameTypeDescription
key (required)String

UID of the field to sort by.

Default: Not applicable
orderby (required)AssetLibrary.ORDERBY

Sort direction: ASCENDING or DESCENDING.

Default: Not applicable

Returns: AssetLibrary. The same asset library, for chaining.

Validation

  • There is no client-side validation of the field UID.
  • A null orderby records nothing, and sort raises no error.
  • A null key removes an earlier sort in the same direction.
  • An invalid field UID surfaces as an Error in the callback when fetchAll sends the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. ASCENDING stores asc=<key>, and DESCENDING stores desc=<key>. fetchAll sends the stored value.
  • The API's Order by asc and Order by desc requests work for assets as well as entries.
  • Each direction has its own key. A second call in the same direction replaces the field, and a call in the other direction adds a second sort parameter. fetchAll then sends both.

Example

Basic usage: list the newest assets first

import android.util.Log;

import com.contentstack.sdk.Asset;
import com.contentstack.sdk.AssetLibrary;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.FetchAssetsCallback;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.List;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    AssetLibrary assetLibrary = stack.assetLibrary();
    assetLibrary.sort("created_at", AssetLibrary.ORDERBY.DESCENDING);
    assetLibrary.fetchAll(new FetchAssetsCallback() {
        @Override
        public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
            if (error == null) {
                for (Asset asset : assets) {
                    Log.d("Contentstack", asset.getFileName() + " at " + asset.getUrl());
                }
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

Ascending order: list the smallest files first

import android.util.Log;

import com.contentstack.sdk.Asset;
import com.contentstack.sdk.AssetLibrary;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.FetchAssetsCallback;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.List;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    AssetLibrary assetLibrary = stack.assetLibrary();
    assetLibrary.sort("file_size", AssetLibrary.ORDERBY.ASCENDING);
    assetLibrary.fetchAll(new FetchAssetsCallback() {
        @Override
        public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
            if (error != null) {
                Log.e("Contentstack", error.getErrorMessage());
                return;
            }
            for (Asset asset : assets) {
                Log.d("Contentstack", asset.getFileName() + ": " + asset.getFileSize() + " bytes");
            }
        }
    });
} catch (Exception e) {
    Log.e("Contentstack", e.getMessage());
}

where

where limits the assets that fetchAll returns to those whose field equals a value.

NameTypeDescription
key (required)String

UID of the field to match.

Default: Not applicable
value (required)String

Value the field must equal.

Default: Not applicable

Returns: AssetLibrary. The same asset library, for chaining.

Validation

  • There is no client-side validation of the field UID or the value.
  • where records nothing when key or value is null, and raises no error.
  • An invalid condition surfaces as an Error in the callback when fetchAll sends the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. where stores the condition {"<key>": "<value>"} as the query parameter, and fetchAll sends it.
  • The API's Equals Operator request, which works for assets as well as entries, returns the assets whose field value matches.
  • Each call replaces the whole condition, so only the last where call reaches the request.
  • where sends value as a JSON string. It cannot send an operator such as $lt or $in, or a number or boolean value.
  • The SDK does not URL-encode the condition. Encode a value that contains & or # with java.net.URLEncoder before you pass it. See Query parameters on the AssetLibrary class page.

Example

Basic usage: list the PNG assets

import android.util.Log;

import com.contentstack.sdk.Asset;
import com.contentstack.sdk.AssetLibrary;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.FetchAssetsCallback;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.List;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    AssetLibrary assetLibrary = stack.assetLibrary();
    assetLibrary.where("content_type", "image/png");
    assetLibrary.fetchAll(new FetchAssetsCallback() {
        @Override
        public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
            if (error == null) {
                for (Asset asset : assets) {
                    Log.d("Contentstack", asset.getFileName() + " at " + asset.getUrl());
                }
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}