Android从公共Dropbox文件夹下载图像

时间:2014-04-03 15:13:36

标签: android dropbox dropbox-api

我的客户希望我从他的公共Dropbox文件夹下载图像并在应用中显示它们。 (想想壁纸应用程序)。有没有办法使用Android的Dropbox sdk?阅读文档我只能找到涉及用户验证自己并使用自己的dropbox帐户的方法。

我所能想到的只是放置一个文本文件,其中包含指向图像的链接并首先下载,然后解析它并从其中的网址下载图像。但是,每次添加新图像时,仍然需要手动编辑和更新文本文件。

2 个答案:

答案 0 :(得分:1)

Dropbox API旨在与经过身份验证的用户一起使用,以对该特定帐户进行API调用。如果您已经拥有需要公开访问的URL,则根本不需要使用API​​,您可以直接从这些URL下载。这些问题可能对此有所帮助:

听起来剩下的问题是您可能不提前知道所有链接,因此使用像您提到的索引文件听起来合理。 (API可以让你列出一个文件夹中的文件,但在我看来,对于这个特定的场景来说,它会有点过分。)

答案 1 :(得分:0)

我几个月前做过similer这样的项目,从dropbox和最新的变化中获取完整的路径。这是我用过的代码。有你的答案,实际上比你想要的更多,因为代码会给你最新的文件夹更改 例如:删除图像

在此代码中我获取了所有文件夹中包含的所有图片网址,并根据需要进行修改

imageLoadingAsyncTask = new AsyncTask<Void, Void, String[]>() {
        ArrayList<String> loadingPaths = new ArrayList<String>();
        ArrayList<String> deletedPathList = new ArrayList<String>();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String[] doInBackground(Void... params) {

            Log.i("start background running", "do in background...");
            Log.d(TAG, "ddd1");
            try {
                DeltaPage<Entry> deltaPage;
                // initializing cursor
                App.initializeCursor(mContext);
                do {

                    deltaPage = mApi.delta(App.getDropBoxCursor(mContext));

                    App.updateCursor(mContext, deltaPage.cursor);
                    if (!deltaPage.reset) {
                        for (DropboxAPI.DeltaEntry<DropboxAPI.Entry> entry : deltaPage.entries) {
                            if (entry.metadata != null) {
                                // if (!entry.metadata.isDeleted) {
                                // Log.d(TAG,
                                // String.format(
                                // "entry.metadata (%d) : %s",
                                // (entry.metadata.contents == null ? 0
                                // : entry.metadata.contents
                                // .size()),
                                // entry.metadata.path));
                                if (!(entry.metadata.isDir)
                                        && entry.metadata.thumbExists) {
                                    loadingPaths.add(entry.metadata.path
                                            .toLowerCase());
                                    Log.i("entry path",
                                            "entry path="
                                                    + entry.metadata.path
                                                            .toLowerCase());
                                }

                            } else {
                                deletedPathList.add(entry.lcPath
                                        .toLowerCase());
                                // filesToRemove.add(entry.lcPath);
                            }
                        }
                    } else {
                        // onDeltaLoadingListener.onReset();
                    }

                } while (deltaPage.hasMore);
            } catch (DropboxIOException e) {
                e.printStackTrace();
                i_activity_dropbox_photo_grid.putExtra("exception", "drop");
            } catch (DropboxException e) {
                e.printStackTrace();
            }
            // ---------------end checking dropbox chenges
            // ////////////////////////// _list2 =
            _list2 = new DropBoxImagePathDataAccess(mContext)
                    .getAppPathList();
            // //// }

            if (deletedPathList.size() > 0) {

                int x = 0, y = 0;
                for (String path : deletedPathList) {
                    // ////////////////////////////////////////////////////////////////////////////////////////////////
                    // path = path.replaceAll("['.]", "");

                    if (_list2.size() > 0 && _list2.contains(path)) {
                        _list2.remove(path);
                        new DropBoxImagePathDataAccess(mContext)
                                .removePath(path);
                        x = 1;
                    }
                    if (loadingPaths.size() > 0
                            && loadingPaths.contains(path)) {
                        loadingPaths.remove(path);
                    }
                    // if the user delete a whole folder
                    // following code will check folder name is contains in
                    // the paths
                    // if contains then whole part is removed, since all the
                    // folder contents also should be remove
                    for (String path1 : _list2) {
                        if (_list2.get(y).contains(path.toLowerCase())) {
                            _list2.remove(y);
                            new DropBoxImagePathDataAccess(mContext)
                                    .removePath(path1);
                        }
                        y++;
                    }
                }
            }

            // remove duplicate paths
            for (String path : _list2) {
                // /////////////////////////////////////////////////////////////////////////////////path
                // = path.replaceAll("[',,]", "");
                if (loadingPaths.contains(path)) {
                    loadingPaths.remove(path);

                }
            }
            int i = 0;
            int size = 0;

            size = _list2.size() + loadingPaths.size();
            mStrings = new String[size];
            // add old paths to mString array
            for (String path : _list2) {
                if (i < size) {
                    mStrings[i] = path;
                    i++;
                }
            }
            // add new paths to mStrings array
            ArrayList<String> updatePaths = new ArrayList<String>();
            if (loadingPaths.size() > 0) {
                for (String newpath : loadingPaths) {
                    if (!_list2.contains(newpath)) {
                        updatePaths.add(newpath);
                        mStrings[i] = newpath;
                        i++;
                    }
                }
            }

            // update shared preference
            if (updatePaths.size() > 0) {
                new DropBoxImagePathDataAccess(mContext)
                        .createPathList(updatePaths);
            }

            Log.i("mString size", "mString size=" + mStrings.length);
            return mStrings;
        }

        @Override
        protected void onPostExecute(String[] result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            finalImageList = result;
            int i1 = 0;
            for (String path : finalImageList) {
                Log.i("final Image list", "final image list" + " i= " + i1
                        + " path= " + path);
            }
            App.DROP_BOX_ALL_PHOTO_LOCATED = true;

            i_activity_dropbox_photo_grid.putExtra("finalList", finalImageList);
            startActivity(i_activity_dropbox_photo_grid);
            overridePendingTransition(R.anim.activity_from_right,
                    R.anim.activity_to_left);
            finish();

        }
    };
相关问题