我正在使用quickblox开发一个Android应用程序,我使用自定义对象为每个用户存储一个文件。 我做多个下载请求,我下载多个图像,所有文件都正确下载,但我需要知道每个下载文件的用户ID ...我尝试从回调中获取此信息,但我无法获取有关事件的此信息每个下载文件的onComplete。 有一种方法可以了解用户所有者吗?
这是我的代码:
QBCustomObjectRequestBuilder requestBuilder = new QBCustomObjectRequestBuilder();
requestBuilder.setPagesLimit(100);
requestBuilder.or("idutente", lista);
QBCustomObjects.getObjects("foto", requestBuilder, new QBCallbackImpl() {
@Override
public void onComplete(Result result) {
if (result.isSuccess()) {
QBCustomObjectLimitedResult coresult = (QBCustomObjectLimitedResult) result;
final ArrayList<QBCustomObject> co = coresult.getCustomObjects();
for (int i = 0 ; i < co.size() ; i++) {
QBCustomObject qbCustomObject = new QBCustomObject("foto", String.valueOf(co.get(i).getCustomObjectId()));
QBCustomObjectsFiles.downloadFile(qbCustomObject, "immagine", new QBCallbackImpl() {
@Override
public void onComplete(Result result) {
QBFileDownloadResult downloadResult = (QBFileDownloadResult) result;
if (result.isSuccess()) {
此处我需要知道下载文件的用户名
// extract file
byte[] content = downloadResult.getContent(); // that's downloaded file content
InputStream is = downloadResult.getContentStream(); // that's downloaded file content
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/quenchat/");
dir.mkdirs();
File cacheFile = new File(dir, "profilo_"+downloadResult.toString()+".png");
BufferedOutputStream bos;
try {
bos = new BufferedOutputStream(new FileOutputStream(cacheFile));
bos.write(content);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
}
}
});
谢谢
答案 0 :(得分:1)
您可以将上下文用于此目的
在此处了解有关上下文的更多信息http://quickblox.com/developers/Android#Performing_actions_with_context
正确答案是:
QBCustomObject qbCustomObject = new QBCustomObject("foto", String.valueOf(co.get(i).getCustomObjectId()));
String userID = String.valueOf(qbCustomObject.getUserId());
QBCustomObjectsFiles.downloadFile(qbCustomObject, "immagine", new QBCallbackImpl() {
@Override
public void onComplete(Result result, Object context) {
QBFileDownloadResult downloadResult = (QBFileDownloadResult) result;
if (result.isSuccess()) {
int userID = Integer.parseInt((String)context);
}
}
}, userID);