2014-02-27 35 views
1

我正在开发一个使用quickblox的android应用程序,我使用自定义对象为每个用户存储一个文件。 我做了一个多重下载请求,在那里我下载多个图像,所有文件都正确下载,但我需要知道每个下载的文件的用户ID ...我尝试从回调中获取此信息,但我无法获取此信息事件onComplete为每个下载的文件。 有一种方法可以知道用户的所有者?Quickblox,如何从自定义对象的下载文件中获取用户ID

这里是我的代码:

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()) { 

在这里,我需要知道文件的用户ID DOWNLOADED

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


            } 
           } 
          }); 
         } 

        } 
       } 
      }); 

谢谢

+1

您可以将用户ID添加到文件名和上传文件。当你完成下载时,子字符串的用户ID。简单 – Xander

+0

好的,但是如何在下载完成后获取文件名?我可以从downloadResult获取? –

+0

在您的OnComplete方法中,了解它是否来自下载。然后您可以从结果中访问文件名。 – Xander

回答

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

如何使用Quickblox在android上下载视频内容? –

相关问题