Search code examples
androiddropboxcloudrail

Upload/download files CloudRail Android/Dropbox


I'm working with CloudRail in order to upload/download files from dropbox to my android device. I don't know how to implement the upload or the download methods. I'm getting lost with the file path directories when it comes to create a simple files.txt to upload.

What I would like to do is write/read a simple String variable into a file.txt and upload it to dropbox. Creating the files in the external memory of the device and then uploading them would be an option too.

I have also made some research with CloudRail samples at GitHub, but there's a lot of code regarding the visual interface, which I don't need, and makes it difficult for me to find a solution. I also found some related post to my needs, but I couldn't reproduce it. Moreover, I posted in the CloudRail forum with no replies.

Thank you in advance for your time

    private void uploadItem(final String name, final Uri uri) {
    startSpinner();
    new Thread(new Runnable() {
        @Override
        public void run() {
            InputStream fs = null;
            long size = -1;
            try {
                fs = getOwnActivity().getContentResolver().openInputStream(uri);
                size = getOwnActivity().getContentResolver().openAssetFileDescriptor(uri, "r").getLength();
            } catch (Exception e) {
                stopSpinner();
                getOwnActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, "Unable to access file!", Toast.LENGTH_SHORT).show();
                    }
                });
                return;
            }

            String next = currentPath;
            if(!currentPath.equals("/")) {
                next += "/";
            }
            next += name;
            getService().upload(next, fs, size, true);

            getOwnActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    updateList();
                }
            });
        }
    }).start();
}

Solution

  • The upload function using CloudRail SDK uses 4 parameters in total as described in our documentation:

    /**
     * @param path The destination path for the new file
     * @param stream The file content that will be uploaded
     * @param size The file size measured in bytes
     * @param overwrite Indicates if the file should be overwritten. Throws an error if set to false and the specified file already exists
     * @throws IllegalArgumentException Null or malformatted path, null stream or negative file size
     */
    void upload(
        String path,
        InputStream stream,
        Long size,
        Boolean overwrite
    );
    

    As seen above the stream parameter should point to the source bytes that should be uploaded, in your case the stream parameter is currently NULL. It doesn't matter where the stream comes from (SD card, disk, memory, etc...) as long as the resulting bytes are sent in the stream parameter. A possible solution for your code would be: (assuming that the file created exists and its successfully loaded)

    File temp = new File(context.getFilesDir(), String.valueOf(System.nanoTime()));
            InputStream stream = new FileInputStream(temp);;
            long size = temp.length();
            dropbox.upload("/TestFolder",stream,size,true);