我是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());
}
}
});
}
如果userList大小隻是1個用戶,會得到什麼結果?包裝「getDatainBackground」的循環「for users」不可能在所有IMO中都很好地擴展。我會查看獲取用戶照片的url列表,然後花費一些時間優化網絡GET和存儲爲解析文件的照片URL列表上的Bmp.decode。 – 2014-09-11 22:58:07
http://stackoverflow.com/a/13311731/560435 – 2014-09-11 23:00:35
@RobertRowntree對不起,我不明白你的回覆。你的意思是我需要使用Loader來查詢用戶,然後爲每個人存儲ParseFile對象'ParseFile file =(ParseFile)user.get(「picture」);'。但是,我需要運行'getDataInBackground()',我該在哪裏做? – Newben 2014-09-13 21:25:24