SimpleSample-content-android

Ask tech team
From QuickBlox Developers (API docs, code samples, SDK)
Jump to: navigation, search

Contents

Sources

Project homepage on GIT — https://github.com/QuickBlox/quickblox-android-sdk/tree/master/sample-content

Download ZIP - https://github.com/QuickBlox/quickblox-android-sdk/archive/master.zip


Overview

This sample demonstrates how to work with QuickBlox Content API.
It allows to organize user's photo gallery.

It shows how to:

  1. Download user's images
  2. Upload new image


Guide: Get Started with Content API

Get QuickBlox account

http://admin.quickblox.com/register

Create application on Admin panel

http://admin.quickblox.com/apps/new

Also you can look through 5 min guide.

Connect QuickBlox to your application

To get the information on how to connect to the quickblox.jar, please, refer to the Android-how-to-connect-quickblox-jar page.

Add Content Management System (CMS) to your application

QuickBlox Content API is:

  • Content Management System (CMS) - Allows you to manage the apps contents and settings without having to re-publish it. Using a web interface you can control and make instant changes to the apps.
  • Unlimited Data Storage
  • Client SDKs that provide access to your content from code

Content Management System (CMS)

CMS allows you to manage the apps contents and settings without having to re-publish it. You can upload any files through web interface, edit them, delete - all typical operations.

Go to Admin panel, Content module. You can see list of your files:

CMS1.png

You may not have files. To upload new file just use File Upload section bellow table - select file to upload using Choose... button, then press Upload button - new file will be uploaded.

You can create rich HTML file using wysiwyg redactor - just press Add Text/HTML button - 'New file' page will be opened. You can use wysiwyg redactor to create rich content.

Upload file

To upload file using Android SDK use code bellow:

// get file
int fileId = R.raw.sample_file;
InputStream is = context.getResources().openRawResource(fileId);
File file = FileHelper.getFileInputStream(is, "sample_file.txt", "myFile");
 
Boolean isPublic = false;
 
QBContent.uploadFileTask(file, isPublic, new QBEntityCallback<QBFile>() {
    @Override
    public void onSuccess(QBFile qbFile, Bundle params) {
 
    }
 
    @Override
    public void onError(QBResponseException error) {
 
    }
}, new QBProgressCallback() {
    @Override
    public void onProgressUpdate(int progress) {
 
    }
});

Read more about isPublic: argument here.


It's possible also to update file's data without creating new file:

// get file
int fileId = R.raw.sample_file;
InputStream is = context.getResources().openRawResource(fileId);
File newFile = FileHelper.getFileInputStream(is, "sample_file.txt", "myFile");
 
int quickBloxFileID = 91079;
 
QBContent.updateFileTask(newFile, quickBloxFileID, new QBEntityCallback<QBFile>() {
    @Override
    public void onSuccess(QBFile qbFile, Bundle params) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
}, new QBProgressCallback() {
    @Override
    public void onProgressUpdate(int progress) {
 
    }
});

Retrieve files

You can use page parameters like:

  • perPage - how many files will contain each page (max 100)
  • page - current returned page

To retrieve list of own files use code bellow:

QBPagedRequestBuilder requestBuilder = new QBPagedRequestBuilder(5, 2);
 
QBContent.getFiles(requestBuilder, new QBEntityCallback<ArrayList<QBFile>>() {
    @Override
    public void onSuccess(ArrayList<QBFile> files, Bundle params) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});


Download file

You can download a file by ID or UID.

Download file by ID

final int fileId = 3607775;
 
QBContent.downloadFileById(fileId, new QBEntityCallback<InputStream>() {
    @Override
    public void onSuccess(final InputStream inputStream, Bundle params) {
        long length = params.getLong(Consts.CONTENT_LENGTH_TAG);
        Log.i(TAG, "content.length: " + length);
 
        // use inputStream to download a file
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
}, new QBProgressCallback() {
    @Override
    public void onProgressUpdate(int progress) {
 
    }
});

Download file by UID

final String fileUID = "6221dd49a1bb46cfb61efe62c4526bd800";
 
QBContent.downloadFile(fileUID, new QBEntityCallback<InputStream>(){
    @Override
    public void onSuccess(InputStream inputStream, Bundle params) {
        long length = params.getLong(Consts.CONTENT_LENGTH_TAG);
        Log.i(TAG, "content.length: " + length);
 
        // use inputStream to download a file
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
}, new QBProgressCallback() {
    @Override
    public void onProgressUpdate(int progress) {
 
    }
});

Public/Private urls

To access a private url use the following method:

QBFile file = ...;
String privateUrl = file.getPrivateUrl();
 
// or if you have only file UID
String fileUID = "6221dd49a1bb46cfb61efe62c4526bd800";
String privateUrl = file.getPrivateUrlForUID(fileUID);


To access a public url use the following method:

QBFile file = ...;
String publicUrl = file.getPublicUrl();
 
// or if you have only file UID
String fileUID = "6221dd49a1bb46cfb61efe62c4526bd800";
String privateUrl = file.getPublicUrlForUID(fileUID);