2013-01-24 14 views
2
獲取文件

這是Android的少的問題,更Parse.com的問題,但我需要你的幫助:從的parseObject

我在Parse.com

表中有一個圖像文件在我的應用程序的文件是好的,我可以手動下載並觀看。

但是,試圖開始使用的parseObject我alreay從服務器得到它的時候,我得到「空」

代碼:

byte[] imageFile = parseObject.getBytes(Statics.COL_IMAGE_FILE); 

我在做什麼錯?

p.s. :我可以在parseObject「estimatedData」字段中看到圖像文件數據/鏈接,但無法找到他。

回答

8

如果Statics.COL_IMAGE_FILEFile列,你應該做到以下幾點得到的byte []:

ParseFile imageFile = (ParseFile)parseObject.get(Statics.COL_IMAGE_FILE); 
imageFile.getDataInBackground(new GetDataCallback() { 
    public void done(byte[] data, ParseException e) { 
    if (e == null) { 
     // data has the bytes for the image 
    } else { 
     // something went wrong 
    } 
    } 
}); 
+0

當我使用的代碼我得到一個空指針異常 – user1708134

+0

我也一樣,也得到了空指針錯誤 – acoustic

+0

的代碼工作正常。也許你將字節數組轉換爲位圖是錯誤的。這正是我在做:) – VipulKumar

4

見從我的應用程序的代碼片段。外部查詢返回文件關聯的對象,內部查詢返回該對象的文件。希望能幫助到你。

ParseQuery<ParseObject> query = ParseQuery.getQuery("ClassName"); 
    query.whereEqualTo("Column", "Your_variable"); 
    query.getFirstInBackground(new GetCallback<ParseObject>() { 
     public void done(ParseObject object, ParseException e) { 
     if (object != null) { 

      ParseFile file = (ParseFile)object.get("File_Coulumn"); 
      file.getDataInBackground(new GetDataCallback() { 


      public void done(byte[] data, ParseException e) { 
       if (e == null) { 

        bitmap=BitmapFactory.decodeByteArray(data, 0, data.length); 
        //use this bitmap as you want 

       } else { 
        // something went wrong 
       } 
       } 
      }); 

     } else { 
      Toast.makeText(getApplicationContext(), "Exception", Toast.LENGTH_SHORT) .show(); 

     } 
     } 
    });