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;
}

したがって、上記の回避策は基本的に、DropboxException を生成する OnProgess メソッドで mOutputStream を閉じます。これよりも優れた/よりクリーンな方法はありますか?

4

1 に答える 1