0
votes

EDIT: I wrote a function passing the Authenticated Box Client and Bitmap Bytestream. Was also informated the root directory on Box has an ID of 0. But I'm still unable to upload.

What I need to do is create a directory, if present don't and upload the file in that directory.

private void doUpload(final BoxAndroidClient client, final ByteArrayInputStream bs) {


    if(client.isAuthenticated())
    {
        System.out.println("Trying to upload: In upload function");

         AsyncTask<Null, Integer, Null> task = new AsyncTask<Null, Integer, Null>() {

                @Override
                protected void onPostExecute(Null result) {
                    Toast.makeText(PagerActivity.this, "done uploading", Toast.LENGTH_LONG).show();
                    super.onPostExecute(result);
                }

                @Override
                protected void onPreExecute() {
                    Toast.makeText(PagerActivity.this, "start uploading", Toast.LENGTH_LONG).show();
                    super.onPreExecute();
                }

                @Override
                protected Null doInBackground(Null... params) {


                    try {
                        BoxDefaultRequestObject folreqObj = new BoxDefaultRequestObject();

                        BoxFolder newFol = new BoxFolder();
                        String parentId = newFol.getId();

                        BoxFileUploadRequestObject requestObj = BoxFileUploadRequestObject.uploadFileRequestObject("0", "bestphotonow.png", bs);
                        client.getFilesManager().uploadFile(requestObj);
                        /*
                        if(client.getFoldersManager().getFolder(parentId, folreqObj).getName()!=null)
                        {
                            BoxFileUploadRequestObject requestObj = BoxFileUploadRequestObject.uploadFileRequestObject(parentId, "bestphotonow.png", bs);
                            client.getFilesManager().uploadFile(requestObj);

                        }
                        else

                        {
                            BoxFolderRequestObject upreqObj = BoxFolderRequestObject.createFolderRequestObject("BestPhotoNow", parentId);
                            client.getFoldersManager().createFolder(upreqObj);
                            BoxFileUploadRequestObject requestObj = BoxFileUploadRequestObject.uploadFileRequestObject(parentId, "bestphotonow.png", bs);
                            client.getFilesManager().uploadFile(requestObj);
                        }*/




                    } catch (BoxRestException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (BoxJSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (BoxServerException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (AuthFatalFailureException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }



                    return null;
                }
            };
            task.execute();
        }


}

So, I have a wallpaper app that fetches some images from a server. I'm trying to integrate box to be able to upload the picture in the current view (viewpager) to Box via Box Android API v2.

https://github.com/box/box-android-sdk-v2

I have been successful to authenticate the user via oauth. But, I'm really stuck at trying to upload the bitmap or uri direct to their upload call without having to launch a folder picker activity. This is what I have.

On click of a button for upload:

case R.id.boxcloudbutton:
        imageLoader.loadImage(url, new SimpleImageLoadingListener(){
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

                Intent intent = OAuthActivity.createOAuthActivityIntent(getApplicationContext(), "vfoj4o6nxqblahh454wem", "J9TQWyFSblahV2C", false, "http://localhost");
                intent.putExtra("boximageUri", imageUri);
                System.out.println("BoxUri: "+imageUri);
                startActivityForResult(intent, BOX_RESULT);
                super.onLoadingComplete(imageUri, view, loadedImage);
            }
        });
        break;

I'm trying to pass either the bitmap or uri

and in the OnActivityResult

    case BOX_RESULT:
             if (resultCode == Activity.RESULT_CANCELED) {
                // Get the error message for why authentication failed.
                String failMessage = data.getStringExtra(OAuthActivity.ERROR_MESSAGE);
                // Implement your own logic to handle the error.
              // handleFail(failMessage);
            } else {
                // You will get an authenticated oath token object back upon success.
                BoxAndroidOAuthData oauth = data.getParcelableExtra(OAuthActivity.BOX_CLIENT_OAUTH);
                        // If you don't want to supply a customized hub or parser, use null to fall back to defaults.
                        BoxAndroidClient client = new BoxAndroidClient( "vfoj4oxxxxxxxxxxtordlh454wem", 
                                "J9TjxxxxxxxmRwux2V2C", null, null, null);
                        client.authenticate(oauth);
                       System.out.println("Data for Box: "+data.getExtras().getString("boximageUri"));
                       BoxFileUploadRequestObject requestObj = BoxFileUploadRequestObject.uploadFileRequestObject(0, "name", file);
                            BoxFile bFile = client.getFilesManager().uploadFile(requestObj);


            }

            break;

Here, I successfully get authenticated, but I do not recieve the uri or the bitmap in the Intent data that I passed.

More, if anyone has any idea of how I can pass bitmap to a file for a file upload for Box api v2, I would really appreciate some assistance!

Thanks, Arnab

1

1 Answers

0
votes

Try shifting around the responsibility....

When you go to the Oauth activity , do just the authentication and return the credentials or Token from that which grants permission for subsequent actions on Box api.

Choose correct api to POST a bitmap... Here you want an api call that allows the ByteArray corresponding to the bitMap to be loaded as a ByteArrayEntity in the body of a http POST action.

This answer sample of how to prepare a local bitmap instance for being wrapped in body of Post as byteArray.