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.

CMS has great tag system - you can share all your files for users groups. Just press on ID column in table to choose what file you want to edit - 'Edit file' page will pe opened:

CMS2.png

Enter users' tags to which you want allow access through API to this file (you can set tag to user through Users module)


Next, let's show how to work with Content API through Android SDK:

Upload file

In order to upload any files you must be logged in and to act on user behalf - please refer to Android Users API documentation.

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 fileIsPublic = true;
 
QBRequestCanceler requestCanceler = QBContent.uploadFileTask(file, fileIsPublic, new QBCallbackImpl() {
    @Override
    public void onComplete(Result result) {
        if (result.isSuccess()) {
            // File uploaded, do something
 
            QBFileUploadTaskResult fileUploadTaskResultResult = (QBFileUploadTaskResult) result;
            QBFile qbFile = fileUploadTaskResultResult.getFile();
 
            // File public url. Will be null if fileIsPublic=false in query
            String publicUrl = qbFile.getPublicUrl();
 
         } else {
             Log.e("Errors",result.getErrors().toString());     
         }
     }
});

Is it 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 QBCallbackImpl() {
    @Override
    public void onComplete(Result result) {
        if (result.isSuccess()) {
            Log.i(TAG, ">>> file updated successful");
        } else {
            Log.e("Errors",result.getErrors().toString());
        }
    }
});

Retrieve files

You can use page parameters like:

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


Retrieve own files

To retrieve list of own files use code bellow:

QBPagedRequestBuilder requestBuilder = new QBPagedRequestBuilder(5, 2);
 
QBContent.getFiles(requestBuilder, new QBCallbackImpl() {
    @Override
    public void onComplete(Result result) {
        if (result.isSuccess()) {
            QBFilePagedResult qbFilePagedResult = (QBFilePagedResult) result;
            Log.d("File list:",qbFilePagedResult.getFiles().toString());
        } else {
            Log.e("Errors",result.getErrors().toString());
        }
    }
});


Retrieve tagged files

It's possible to share files between users - just update file and set user's tag. Next, users with this tag will be able to request tagged files:

QBPagedRequestBuilder requestBuilder = new QBPagedRequestBuilder(15, 2);
 
QBContent.getTaggedList(requestBuilder, new QBCallbackImpl() {
    @Override
    public void onComplete(Result result) {
        if (result.isSuccess()) {
            QBFilePagedResult qbFilePagedResult = (QBFilePagedResult) result;
            Log.d("File list:",qbFilePagedResult.getFiles().toString());
        } else {
            Log.e("Errors",result.getErrors().toString());
        }
    }
});

Download file

To download any file just use code bellow:

// Download file with ID 126
QBContent.downloadFileTask(126, new QBCallbackImpl() {
    @Override
    public void onComplete(Result result) {
 
        QBFileDownloadResult qbFileDownloadResult = (QBFileDownloadResult) result;
        if (result.isSuccess()) {
            byte[] content = qbFileDownloadResult.getContent();       // that's downloaded file content
            InputStream is = qbFileDownloadResult.getContentStream(); // that's downloaded file content
 
            Log.d("file downloaded successful", qbFileDownloadResult.getContent().toString());
        } else {
            Log.e("Errors",result.getErrors().toString());
        }
    }
});


Comments

Feel free to comment on this page using the form below.

blog comments powered by Disqus