box / box-android-sdk

Apache License 2.0
62 stars 74 forks source link

How to get ID of uploaded file? #434

Closed DavidVasquezMindstrong closed 3 years ago

DavidVasquezMindstrong commented 3 years ago

Hello

I'm uploading files using the sdk com.box:box-android-sdk:5.0.0. After the file has been successfully uploaded, I try to get the file ID but it always returns null.

        private BoxRequestUpload uploadFile;
        private BoxFutureTask boxFutureTask;

        BoxAuthentication.BoxAuthenticationInfo info = new BoxAuthentication.BoxAuthenticationInfo();
        info.setAccessToken(token);

        BoxSession session = new BoxSession(context, info, null);
        session.authenticate(context, response -> {
            if (response.isSuccess()) {
                try {
                    BoxApiFile fileApi = new BoxApiFile(session);
                    InputStream inputStream = context.getContentResolver().openInputStream(uri);
                    if (!reUpload) {
                        // brand new file upload
                        uploadFile = fileApi.getUploadRequest(inputStream, filename, folderId);
                    } else {
                        // update file version
                        uploadFile = fileApi.getUploadNewVersionRequest(inputStream, existingFileId);
                    }
                    uploadFile.setUploadSize(totalBytes);
                    boxFutureTask = uploadFile.setProgressListener((numBytes, totalBytes) -> {
                       // update progress bar
                    }).toTask();
                    boxFutureTask.run();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

After the file is uploaded, I use uploadFile.getId(), but this always returns null

Can anyone help me to solve how to get the ID of the file?

Thank you

sujaygarlanka commented 3 years ago

@DavidVasquezMindstrong I first recommend using the Android SDK version 4.2.3 found here. I apologize, but version 5.0.0 is a a beta. Also, I recommend trying to a run simple upload command as shown in our docs here. If this works, then I would try doing what you are doing with BoxFutureTask. Also, some sample code can be found here in our sample app. I hope this helps!

DavidVasquezMindstrong commented 3 years ago

Thanks for the response. The samples helped me to get to the solution. I ended up adding a completion listener to the future task

boxFutureTask.addOnCompletedListener(boxResponse -> {
     if (boxResponse.isSuccess()) {
          fileId = ((BoxFile) boxResponse.getResult()).getId();
     }
});

Thanks again