在skydrive SDK Android中获取文件ID

时间:2013-07-11 08:27:50

标签: android onedrive

我正在制作一款可以从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 以使此代码正常工作。如果有一些例子

,那真是太棒了

1 个答案:

答案 0 :(得分: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;
}