【问题标题】:issue when getting back image from Android Parse API从 Android Parse API 取回图像时的问题
【发布时间】:2014-11-04 16:53:34
【问题描述】:

我是 Parse 和 Android API 的新手。我想获取我的用户的图片,但问题是在获取这些图片后我无法获取这些图片。准确的说,我用的是Loader,在onCreateLoader中取到图片后,在onLoadFinished方法中取不回图片。

我该如何管理?

当前代码:

public Loader<List<ParseUser>> onCreateLoader(int id, Bundle args) {
    return new ThrowableLoader<List<ParseUser>>(getActivity(), users) {
        // where ThrowableLoader simply extends AsyncLoader which implements loadData
        @Override
        public List<ParseUser> loadData() throws Exception {
            try {
                if(getActivity() != null) {
                    ParseQuery<ParseUser> query = ParseUser.getQuery();
                    query.orderByAscending(Constants.ParseConstants.KEY_USERNAME);
                    query.setLimit(10);
                    users = query.find();
                    bitmap = new Bitmap[users.size()];

                    for(ParseUser user : users) {
                        ParseFile file = (ParseFile) user.get("picture");
                        if(file != null)
                        file.getDataInBackground(new GetDataCallback() {
                            public void done(byte[] data, ParseException e) {
                                if (e == null) {
                                    bitmap[0] = BitmapFactory.decodeByteArray(data, 0, data.length); // here I put this bitmap[0] as a test purpose
                                }
                                else {
                                    System.out.println("Loading image failed for the user" + e.getMessage());
                                }
                            }
                        });
                    }

【问题讨论】:

  • 如果 userList 的大小只有 1 个用户,你会得到什么结果?包装“getDatainBackground”的“用户”循环不太可能在所有 IMO 中很好地扩展。我会查看获取用户照片的 url 列表,然后花一些时间优化网络 GET 和存储为解析文件的照片 URL 列表上的 Bmp.decode。
  • @RobertRowntree 抱歉,我不明白您的回复。你的意思是我需要使用 Loader 来查询用户,然后为每个用户存储 ParseFile 对象ParseFile file = (ParseFile) user.get("picture");。但是,我需要运行getDataInBackground(),我在哪里做呢?

标签: java android android-asynctask parse-platform


【解决方案1】:

您在循环中有性能问题还是其他类型的缺陷?

IMO - 代码中的内循环

 file.getDataInBackground

由于与可扩展性和解析 SDK 如何实现支持“InBackground”的 AsyncTask 细节有关的几个原因而脱颖而出。我链接到另一个线程,该线程解释了为什么 LOOP 可能不是一个好主意,然后在循环中调用“inBackground”类型的东西,因为它们在 AsnycTask 部分上可能没有高优先级,或者它们可能仍然是单线程,你只会陷入循环结构,使用差异循环管理器可以更快地运行。

如果您只有一个用户并且代码运行正常,这可能意味着没有更大的数组(必须循环遍历以下迭代步骤)就可以了:

获取图片对应的URL

将 GET 的响应流解码为位图

我建议您考虑获取 URL 列表,然后

使用多个连接的线程池

Get the Photo
pass the response stream to a bitmap decoder

我使用 parse 对此进行了测试,并使用 Parse 的 AsnycTask 版本在循环器中获得了 3 秒 vs 8 分钟,我知道当时它是单线程的。

【讨论】:

  • 感谢您的详细回复,但很抱歉我对 Android 很陌生,您能否提供一些有关 threadPool of Multiple Connections 的文档的链接?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-05
  • 1970-01-01
  • 1970-01-01
  • 2017-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多