2014-09-10 65 views
0

我是Parse和Android API的新手。我想为我的用户提取图片,但问题是,我们在获取这些图片后无法获取这些图片。确切地说,我使用Loader,在onCreateLoader中取回方法后,我无法取回方法onLoadFinished中的图片。从Android Parse API获取图像时出现问题

我该如何管理?

当前代码:

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()); 
           } 
          } 
         }); 
        } 
+0

如果userList大小只是1个用户,会得到什么结果?包装“getDatainBackground”的循环“for users”不可能在所有IMO中都很好地扩展。我会查看获取用户照片的url列表,然后花费一些时间优化网络GET和存储为解析文件的照片URL列表上的Bmp.decode。 – 2014-09-11 22:58:07

+0

http://stackoverflow.com/a/13311731/560435 – 2014-09-11 23:00:35

+0

@RobertRowntree对不起,我不明白你的回复。你的意思是我需要使用Loader来查询用户,然后为每个人存储ParseFile对象'ParseFile file =(ParseFile)user.get(“picture”);'。但是,我需要运行'getDataInBackground()',我该在哪里做? – Newben 2014-09-13 21:25:24

回答

0

你在循环中有性能问题或者是一些其他类型的缺陷?

IMO - 在你的代码

file.getDataInBackground 
内环

脱颖而出与可扩展性几个原因,并的解析SDK如何实现了备份的“InBackground”的AsyncTask的详细细节。我链接到另一个线程,进入为什么它可能不是一个好主意,然后在循环调用“inBackground”类型的东西,因为他们可能没有高优先级的AsnycTask片断,或者他们可能仍然是单线程,你会陷入循环结构中,使用diff循环管理器可以更快地运行。

如果只有一个用户和代码运行正常,这可能意味着其正常的,而不具有循环直通的迭代步骤的更大的阵列:

获取URL对应的照片

解码响应在得到一个位图流

我建议你考虑得到的URL列表,然后

使用多个连接

的线程池3210
Get the Photo 
pass the response stream to a bitmap decoder 

我在parse中使用Parse的AsnycTask版本进行了解析并获得了3秒钟和8分钟的测试,我知道那时候是单线程的。

+0

感谢您的详细回复,但很抱歉,我对Android非常陌生,您是否可以提供一些链接,指向一些有关'多个连接的threadPool'的文档? – Newben 2014-09-13 22:41:33

相关问题