ownCloud Android DownloadRemoteFile文件名

时间:2015-07-07 21:10:45

标签: android owncloud

我尝试使用ownCloud在Android中下载RemoteFile列表。我可以完全下载文件,但我想在文件完成时通知用户。我正在下载整个目录:

@Override
public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
    if (operation instanceof ReadRemoteFolderOperation) {
        if (result.isSuccess()) {
            Toast.makeText(this, "Finished reading folder", Toast.LENGTH_SHORT).show();
            for (Object o : result.getData()) {
                RemoteFile remoteFile = (RemoteFile) o;
                String remotePath = remoteFile.getRemotePath();

                File targetDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
                        "/owncloud_download");

                downloadHelper.downloadFile(remoteFile, targetDirectory);
            }
        }
    }

    if (operation instanceof DownloadRemoteFileOperation) {
        if (result.isSuccess()) {
            // Notify the user here that the file finished
        }
    }
}

我查看了ownCloud库源,但似乎无法找到DownloadRemoteFileOperation返回的结果,而不是指示成功的布尔值和HTTP状态代码。我以为它可能在result.getLogMessage(),但这只是给我一个HTTP 200状态。如何获取已完成文件的名称?

编辑:我还查看了result.getData(),但在DownloadRemoteFileOperation中显示为空。

1 个答案:

答案 0 :(得分:0)

这是我暂时的解决方法。我不想再修改ownCloud库源代码,所以我只是像onTransferProgress那样检查一下:

@Override
public void onTransferProgress(long rate, long transferred, long total, String fileName) {
    if (transferred == total) {
        runOnUiThread(new Runnable() {
            // do the update here, file name is available
        }
    }
}

这是另一种选择。如果上传失败,我需要上传文件,因此我修改了ownCloud库源。这样我就可以在RemoteOperationResult中返回文件名。

RemoteOperationResult.java:

private String fileName;

public String getFileName() {
    return fileName;
}

public void setFileName(String name) {
    fileName = name;
}

DownloadRemoteFileOperation.java

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;

    /// download will be performed to a temporal file, then moved to the final location
    File tmpFile = new File(getTmpPath());

    /// perform the download
    try {
        tmpFile.getParentFile().mkdirs();
        int status = downloadFile(client, tmpFile);
        result = new RemoteOperationResult(isSuccess(status), status,
                (mGet != null ? mGet.getResponseHeaders() : null));
        Log_OC.i(TAG, "Download of " + mRemotePath + " to " + getTmpPath() + ": " +
                result.getLogMessage());

    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log_OC.e(TAG, "Download of " + mRemotePath + " to " + getTmpPath() + ": " +
                result.getLogMessage(), e);
    }
    // Added this line
    result.setFileName(mRemotePath);

    return result;
}

UploadRemoteFileOperation.java:

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;

    try {
        // / perform the upload
        synchronized (mCancellationRequested) {
            if (mCancellationRequested.get()) {
                throw new OperationCancelledException();
            } else {
                mPutMethod = new PutMethod(client.getWebdavUri() +
                        WebdavUtils.encodePath(mRemotePath));
            }
        }

        int status = uploadFile(client);
        if (mForbiddenCharsInServer){
            result = new RemoteOperationResult(
                    RemoteOperationResult.ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER);
        } else {
            result = new RemoteOperationResult(isSuccess(status), status,
                    (mPutMethod != null ? mPutMethod.getResponseHeaders() : null));
        }
    } catch (Exception e) {
        // TODO something cleaner with cancellations
        if (mCancellationRequested.get()) {
            result = new RemoteOperationResult(new OperationCancelledException());
        } else {
            result = new RemoteOperationResult(e);
        }
    }
    // Added this line
    result.setFileName(mLocalPath);

    return result;
}