Fetching images from the Bitplaces platform

If you know the ID of a given image (for example, from using the getImageId() accessor on a Bitplace instance) you can fetch it from the backend through the following instance method of the BitplacesSDK class.

1
fetchImage(long imageID, BitplacesOperationCompletionHandler<BitplacesImage> completionHandler)

Upon successful completion of the request, your completion handler will be passed a BitplacesImage instance from which you can extract image data. Images are cached as files in the application's cache directory.

The BitplacesImage class exposes the following relevant methods:

Method Return value type Description
getImageId() long ID of this image on the Bitplaces platform
getFilePath() String Returns the absolute path of the image file. This path can be used to inflate an Android Bitmap using Android's BitmapFactory.decodeFile(filePath) method.
asBitmap() android.graphics.Bitmap As a convenience, the BitplacesImage class can try to inflate a Bitmap itself. If a runtime error is thrown by the system while this operation is performed (notably an OutOfMemoryError) it will be propagated to your calling code, which can catch it and handle it in a way that makes sense to your application logic.
tryAsBitmap() android.graphics.Bitmap Same as asBitmap(), but if an OutOfMemoryError occurs it will be silently caught and a null Bitmap will be returned.
getFileInputStream() java.io.FileInputStream Attempts to open and return a FileInputStream pointing to this image.
getLastThrownError() java.lang.Throwable Returns the last error caught by tryAsBitmap() or getFileInputStream()

If the requested image is already cached your callback will be called immediately by the SDK with the cached image.

You can also ask the SDK if an image is already cached with the isImageCached(imageId) instance method of the BitplacesSDK class, and if you get a positive response you can access the cached BitplacesImage synchronously, without the need of callbacks, through getCachedImage(imageId).

EXAMPLE: CHECKING AND GETTING SYNCHRONOUSLY A CACHED IMAGE

1
2
3
4
5
6
7
8
9
BitplacesSDK bpSDK = BitplacesSDK.getInstance();

if (bpSDK.isImageCached(imageId) {
    BitplacesImage image = bpSDK.getCachedImage(imageId);
    Bitmap bitmap = BitmapFactory.decodeFile(filePath);
    // ... use the bitmap as you need it ...
} else {
    // Retrieve the image from the backend as shown below
}

EXAMPLE: CHECKING AND GETTING ASYNCHRONOUSLY A CACHED IMAGE

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
bpSDK.fetchImage(imageId, new BitplacesOperationCompletionHandler<BitplacesImage>() {
    @Override
    public void onOperationComplete(int resultCode, BitplacesImage image) {
        if (BitplacesSDK.ResultCode.fromCode(resultCode).isSuccessful()) {
            String filePath = image.getFilePath();
        Bitmap bitmap = BitmapFactory.decodeFile(filePath);
        // ... use the bitmap as you need it ...
        } else {
            // Handle the error condition in a way that makes
            // sense to your application logic
        }
    }
});