将文件(图像)上传到quickblox自定义对象

时间:2013-12-28 16:10:03

标签: java android quickblox custom-object

如何将我拥有自己的Uri的文件(图像)上传到自定义对象?

我试过这个:

   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());
                    }
                }
            });

我只得到字符串(“bla”)和图像get - null

谢谢

1 个答案:

答案 0 :(得分:2)

目前此功能正处于开发早午餐阶段。将在几天内掌握。

但您已经可以使用它了 - 从开发早午餐中下载SDK https://github.com/QuickBlox/quickblox-android-sdk/tree/development

以下是许多代码段,尤其是自定义对象模块

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

上传文件

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);
        }
   }
});

下载文件

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);
       }
    }
});

删除文件

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);
        }
    }
});