Search code examples
androidsavedocumentbaasbox

baasbox save document on android API


I am having the following nasty stack trace

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Caused by: class com.baasbox.android.BaasClientException :{"result":"error","message":"The body payload cannot be empty. Hint: put in the request header Content-Type: application/json","resource":"/document/Images","method":"POST","request_header":{"X-BAASBOX-APPCODE":["1234567890"],"Connection":["Keep-Alive"],"Content-Length":["316039"],"X-BB-SESSION":["187be9f8-dc77-436d-89de-65dd3cf0e2ba"],"Content-Type":["application/json;charset=UTF-8"],"Accept-Encoding":["gzip"],"User-Agent":["BaasBox AndroidSDK/0.9.2"],"Host":["192.168.192.59:9000"]},"API_version":"0.9.5"}
at com.baasbox.android.NetworkTask.onClientError(NetworkTask.java:98)
at com.baasbox.android.NetworkTask.parseResponse(NetworkTask.java:63)
at com.baasbox.android.NetworkTask.asyncCall(NetworkTask.java:151)
at com.baasbox.android.impl.Task.execute(Task.java:189)
at com.baasbox.android.impl.Dispatcher$Worker.run(Dispatcher.java:170)

This happens when I attempt to save a picture into a Collection previously created on my local baasbox installation, here is the code that causes the problem, not sure what is the actual issue... any help you may bring will be highly appreciated...

Bitmap bitmap = MediaStore.Images.Media.getBitmap( this.getContentResolver(), selectedImage);
ImageView imageView = (ImageView)findViewById(R.id.shareImageView);
imageView.setImageBitmap(bitmap);

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

BaasDocument doc = new BaasDocument("Images");
doc.put("title","Image");
doc.put("body", byteArray);
doc.save(new BaasHandler<BaasDocument>() {
    @Override
    public void handle(BaasResult<BaasDocument> res) {
        if(res.isSuccess()) {
            Log.d("LOG","Saved: "+res.value());
        } else {
            Log.d("LOG","No Saved: "+res.value());
        }
    }
});

Solution

  • This piece of code solved the problem, instead of treating the image as a document, I should have used a file from the very beginning , any comments on when to use files and when to use documents are more than welcome.

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    
    JsonObject attachedData = new JsonObject();
    attachedData.put("username", BaasUser.current().getName());
    
    BaasFile file = new BaasFile(attachedData);
    
    //BaasACL acl = new BaasACL().grantUsers(Grant.READ,"all");
    
    //file.upload(acl, byteArray,new BaasHandler<BaasFile> () {
    file.upload(byteArray,new BaasHandler<BaasFile> () {
        @Override
        public void handle(BaasResult<BaasFile> res) {
            if( res.isSuccess() ) {
                Log.d("LOG","File uploaded with permissions");
            } else {
                Log.e("LOG","Deal with error",res.error());
            }
        }
    });