【发布时间】: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