Search code examples
javaandroidquickbloxcustom-object

upload file(image) into quickblox custom object


How do I upload file (image) that I have his Uri, into custom object?

I've tried this:

   public void onClick(View v) {

            HashMap<String, Object> fields = new HashMap<String, Object>();

            File newImage = new File(imageUri.toString());
            fields.put("image", newImage);
            fields.put("bla", "bla");


            QBCustomObject qbCustomObject = new QBCustomObject();
            qbCustomObject.setClassName("imageClass");  // your Class name
            qbCustomObject.setFields(fields);
            QBCustomObjects.createObject(qbCustomObject, new QBCallbackImpl() {
                @Override
                public void onComplete(Result result) {
                    if (result.isSuccess()) {
                        QBCustomObjectResult qbCustomObjectResult = (QBCustomObjectResult) result;
                        QBCustomObject qbCustomObject = qbCustomObjectResult.getCustomObject();
                       // Log.d("New record: ",newCustomObject.toString());
                    } else {
                        Log.e("Errors",result.getErrors().toString());
                    }
                }
            });

And I get just the string ("bla") and the image get - null

Thank you


Solution

  • Currently this feature is in development brunch. Will be in master in a couple of days

    But you can use it already - Download SDK from development brunch https://github.com/QuickBlox/quickblox-android-sdk/tree/development

    Here are lots of snippets, especially for Custom Objects module

    https://github.com/QuickBlox/quickblox-android-sdk/blob/development/snippets/src/com/quickblox/snippets/modules/SnippetsCustomObjects.java

    Upload file

    QBCustomObject qbCustomObject = new QBCustomObject(CLASS_NAME, NOTE1_ID);
    QBCustomObjectsFiles.uploadFile(file1, qbCustomObject, AVATAR_FIELD, new QBCallbackImpl() {
        @Override
        public void onComplete(Result result) {
            if (result.isSuccess()) {
                QBCustomObjectFileField customObjectFileField = ((QBCOFileUploadResult) result).getCustomObjectFileField();
                Log.i(TAG, ">>>upload response:" + customObjectFileField.getFileName() + " " + customObjectFileField.getFileId() + " " +
                                    customObjectFileField.getContentType());
            } else {
                handleErrors(result);
            }
       }
    });
    

    Download file

    QBCustomObject qbCustomObject = new QBCustomObject(CLASS_NAME, NOTE1_ID);
    QBCustomObjectsFiles.downloadFile(qbCustomObject, AVATAR_FIELD, new QBCallbackImpl() {
        @Override
        public void onComplete(Result result) {
            QBFileDownloadResult downloadResult = (QBFileDownloadResult) result;
            if (result.isSuccess()) {
                byte[] content = downloadResult.getContent();       // that's downloaded file content
                InputStream is = downloadResult.getContentStream(); // that's downloaded file content
                Log.i(TAG, ">>> file downloaded successfully" + getContentFromFile(is));
                if(is!=null){
                    try{
                        is.close();
                    }catch(IOException e){
                        e.printStackTrace();
                    }
                }
           } else {
               handleErrors(result);
           }
        }
    });
    

    Delete file

    QBCustomObject qbCustomObject = new QBCustomObject(CLASS_NAME, NOTE1_ID);
    QBCustomObjectsFiles.deleteFile(qbCustomObject, AVATAR_FIELD, new QBCallbackImpl() {
        @Override
        public void onComplete(Result result) {
            if (result.isSuccess()) {
                Log.i(TAG, ">>> file deleted successfully");
            } else {
                handleErrors(result);
            }
        }
    });