2014-09-10 36 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

相關問題