2

我正在使用 CloudRail,以便将文件从保管箱上传/下载到我的 android 设备。我不知道如何实现上传下载方法。在创建要上传的简单 files.txt 时,我迷失了文件路径目录。

我想做的是将一个简单的字符串变量写入/读取到 file.txt 并将其上传到保管箱。在设备的外部存储器中创建文件然后上传它们也是一种选择。

我也在 GitHub 上对 CloudRail 示例进行了一些研究,但是关于可视化界面的代码很多,我不需要,这让我很难找到解决方案。我还找到了一些与我的需求相关的帖子,但我无法复制它。此外,我在 CloudRail 论坛上发帖,没有任何回复。

提前感谢您的时间

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

1 回答 1

1

使用 CloudRail SDK 的上传功能总共使用了 4 个参数,如我们的文档中所述:

/**
 * @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
);

如上所示, 流参数应指向应上传的源字节,在您的情况下,流参数当前为NULL。只要在参数中发送结果字节,流来自哪里(SD 卡、磁盘、内存等)都没有关系。您的代码的可能解决方案是:(假设创建的文件存在并且成功加载)

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);
于 2018-05-18T20:51:54.660 回答