2

使用 Android Dropbox SDK 时,有谁知道在 getFile 调用开始后取消下载的最佳方法。目前,我的 AsyncTask 类执行以下操作:

@Override
protected O2ShellFileManagerError doInBackground(File... params) {
    if (params[0] == null) {
        return new O2ShellFileManagerError(
                "No File found.",
                O2ShellErrorCode._o2sfm_ec_unknown);
    }

    File mFile = params[0];

    try {
        DropboxAPI<AndroidAuthSession> mDBApi = O2DropboxStorage
                .getDropboxSession(mContext);

        // mOutputStream is class level field.
        mOutputStream = new FileOutputStream(mFile);
        DropboxFileInfo info = mDBApi.getFile(mDropboxPath, null,
                mOutputStream, new ProgressListener() {

                    @Override
                    public void onProgress(long bytes, long total) {
                        if (!isCancelled())
                            publishProgress(bytes, total);
                        else {
                            if (mOutputStream != null) {
                                try {
                                    mOutputStream.close();
                                } catch (IOException e) {
                                }
                            }
                        }
                    }
                });
    } catch (DropboxException e) {
        Log.e("DbExampleLog",
                "Something went wrong while getting file.");

    } catch (FileNotFoundException e) {
        Log.e("DbExampleLog", "File not found.");

    } finally {
        if (mOutputStream != null) {
            try {
                mOutputStream.close();
                mOutputStream = null;
            } catch (IOException e) {
            }
        }
    }
    /*
     * The task succeeded
     */
    return null;
}

所以我上面的解决方法基本上关闭了 OnProgess 方法中的 mOutputStream,它会生成 DropboxException。有比这更好/更清洁的方法吗?

4

1 回答 1

1

使用 getFileStream API 获取 DropboxInputStream。

public DropboxAPI.DropboxInputStream getFileStream(java.lang.String path, java.lang.String rev)

当用户取消下载时,调用 DropboxInputStream 的关闭函数。

我认为这种方法可以完全释放资源。

于 2013-12-17T02:44:40.730 回答