1

我正在制作一个可以从 skydrive 下载文件的应用程序。我的应用程序使用 Skydrive SDK。我在下面找到了一段代码,可以将文件从 skydrive 下载到 android 设备:

final LiveDownloadOperation operation = mClient.downloadAsync(fileId + "/content", file, new LiveDownloadOperationListener() {
                        @Override
                        public void onDownloadProgress(int totalBytes, int bytesRemaining, LiveDownloadOperation operation) {
                            int percentCompleted = computePrecentCompleted(totalBytes, bytesRemaining);

                            progressDialog.setProgress(percentCompleted);
                        }

                        @Override
                        public void onDownloadFailed(LiveOperationException exception, LiveDownloadOperation operation) {
                            progressDialog.dismiss();
                            showToast(exception.getMessage());
                        }

                        @Override
                        public void onDownloadCompleted(LiveDownloadOperation operation) {
                            progressDialog.dismiss();
                            showToast("File downloaded.");
                        }
                    });

但我不知道在哪里可以从 skydrive 获取fileId以使此代码正常工作。如果有一些例子,那就太棒了

4

1 回答 1

0

我已经为自己找到了解决方案。只需获取所有文件的列表,与查找文件进行比较,然后获取它的 ID。这是我的代码:

public List<DFile> getFileIdFromName(String fileName) {
    DFile res = null;
    // List<DFile> dolphinListFile = getListFile(DIR_HOME);
    // for (DFile dolphin : dolphinListFile) {
    // if (dolphin.getFileName().compareTo(fileName) == 0)
    // res = dolphin;
    // }
    boolean canGetId = true;
    if (!isLoggedIn()) {
        OnGetIdFromFileNameChecked(null, "Not logged in or check session false");
        canGetId = false;
    }
    if (canGetId) {
        mFileList = new ArrayList<DFile>();
        final String mFileName = fileName;
        mClient.getAsync(DIR_HOME + "/files", new LiveOperationListener() {
            @Override
            public void onComplete(LiveOperation operation) {

                JSONObject result = operation.getResult();
                if (result.has(JsonKeys.ERROR)) {
                    JSONObject error = result.optJSONObject(JsonKeys.ERROR);
                    String message = error.optString(JsonKeys.MESSAGE);
                    String code = error.optString(JsonKeys.CODE);
                    Log.d("Dolphin download err", code + ": " + message);
                    return;
                }
                JSONArray data = result.optJSONArray(JsonKeys.DATA);

                for (int i = 0; i < data.length(); i++) {
                    SkyDriveObject skyDriveObj = SkyDriveObject.create(data.optJSONObject(i));

                    skyDriveObj.accept(new Visitor() {
                        @Override
                        public void visit(SkyDriveAlbum album) {
                            mDFile = new DFile(album.getId(), "N/A", album.getName(), DFile.FILEHOST_SKYDRIVE, DFile.FILETYPE_ALBUM);

                        }

                        @Override
                        public void visit(SkyDrivePhoto photo) {
                            mDFile = new DFile(photo.getId(), "N/A", photo.getName(), DFile.FILEHOST_SKYDRIVE, DFile.FILETYPE_PHOTO);
                        }

                        @Override
                        public void visit(SkyDriveFolder folder) {
                            mDFile = new DFile(folder.getId(), "N/A", folder.getName(), DFile.FILEHOST_SKYDRIVE, DFile.FILETYPE_FOLDER);
                        }

                        @Override
                        public void visit(SkyDriveFile file) {
                            mDFile = new DFile(file.getId(), "N/A", file.getName(), DFile.FILEHOST_SKYDRIVE, DFile.FILETYPE_FILE);
                        }

                        @Override
                        public void visit(SkyDriveVideo video) {
                            mDFile = new DFile(video.getId(), "N/A", video.getName(), DFile.FILEHOST_SKYDRIVE, DFile.FILETYPE_VIDEO);
                        }

                        @Override
                        public void visit(SkyDriveAudio audio) {
                            mDFile = new DFile(audio.getId(), "N/A", audio.getName(), DFile.FILEHOST_SKYDRIVE, DFile.FILETYPE_AUDIO);
                        }
                    });
                    if (mDFile.getFileName().compareTo(mFileName) == 0) {
                        mFileList.add(mDFile);
                    }
                }
                OnGetIdFromFileNameChecked(mFileList, "Complete");
            }

            @Override
            public void onError(LiveOperationException exception, LiveOperation operation) {

                Log.d("Dolphin download err", exception.getMessage());
                OnGetListChecked(null, exception.getMessage());
            }
        });
    }
    return mFileList;
}
于 2013-07-18T04:52:23.267 回答