【问题标题】:ownCloud Android DownloadRemoteFile File NameownCloud Android 下载RemoteFile 文件名
【发布时间】:2015-07-07 21:10:45
【问题描述】:

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

@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 库源,但除了指示成功的布尔值和 HTTP 状态代码之外,似乎找不到 DownloadRemoteFileOperation 返回的结果。我认为它可能在 result.getLogMessage() 中,但这只是给了我一个 HTTP 200 状态。如何获取已完成文件的名称?

编辑:我还查看了result.getData(),但在 DownloadRemoteFileOperation 中这是空的。

【问题讨论】:

    标签: android owncloud


    【解决方案1】:

    这是我暂时的解决方法。我不想(再次)修改 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;
    }
    

    下载RemoteFileOperation.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;
    }
    

    上传远程文件操作.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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      相关资源
      最近更新 更多