2015-10-25 111 views
2

我一直停留在這個整天使用Parse.com檢索圖像filess。我嘗試了所有其他可用的方法來完成此操作。下面的方法幾乎完成了這項工作,但是它爲變量文件提供了空指針異常。儲蓄與Android的

下面的代碼描述瞭如何保存數據

  final ParseObject post = new ParseObject("Student"); 
      post.put("name", nameS); 

      final ParseFile file = new ParseFile("photo.png", data); 
      file.saveInBackground(new SaveCallback() { 
       @Override 
       public void done(ParseException e) { 
        post.put("studentPhoto", file); 
        Log.d("John", ""+ file); 
       } 
      }); 

      post.put("author", ParseUser.getCurrentUser()); 

      post.saveInBackground(new SaveCallback() { 
       public void done(ParseException e) { 
        if (e == null) { 
         // Saved successfully. 
         Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show(); 
         Intent intent = new Intent(AddStudentActivityEditText.this, AddStudentActivityTextView.class); 
         startActivity(intent); 

        } else { 
         // The save failed. 
         Toast.makeText(getApplicationContext(), "Failed to Save", Toast.LENGTH_SHORT).show(); 
         Log.d(getClass().getSimpleName(), "User update error: " + e); 
        } 
       } 
      }); 

下面的數據介紹了有關檢索數據

final ParseQuery<ParseObject> query = ParseQuery.getQuery("Student"); 
    query.whereEqualTo("author", ParseUser.getCurrentUser()); 
    query.findInBackground(new FindCallback<ParseObject>() { 
     @Override 
     public void done(List<ParseObject> objects, ParseException e) { 
      if (e == null) { 

       for (ParseObject text : objects) { 

        name.setText(text.getString("name")); 
       } 

       ParseQuery<ParseObject> query = ParseQuery.getQuery("Student"); 
       query.whereEqualTo("author", ParseUser.getCurrentUser()); 
       query.getFirstInBackground(new GetCallback<ParseObject>() { 
        public void done(ParseObject object, ParseException e) { 
         if (object != null) { 

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


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

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

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

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

         } 
        } 
       }); 

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

      } 
     } 
    }); 

回答

1

我不知道爲什麼你的做法並沒有工作,但我將向您展示我從解析中獲取照片的方法。 首先,確保你成功地保存你的形象 - 要做到這一點,去你的應用程序parse.com面板,開核選項卡上,並在你的學生臺檢查是否有一個名爲studentPhoto列,應該有鏈接那張照片。 而我從解析獲取圖像的方法是使用畢加索庫。簡單地說,從解析你應該只鏈接到照片,而不是整個文件,然後使用畢加索下載該照片。 要獲得鏈接到文件做這樣的事情:

ParseObject object = ... // your student object 
try { 
    object.fetchIfNeeded(); 
} catch (ParseException e) { 
    e.printStackTrace(); 
} 

ParseFile avatar = object.getParseFile("studentPhoto"); 
String avatarUrl = avatar.getUrl(); 

然後設置使用畢加索的形象:

Picasso.with(getActivity()) // instead of getActivity() you could pass there any context 
        .load(avatarUrl) 
        .into(yourImageViewWithAvatar); 

有畢加索的網站了解更多信息:http://square.github.io/picasso/

+0

有學生列,但不是有什麼聯繫,也得到了一些東西,它的類型是字節 – johnrao07

+0

嗯,應該有一個學生表,而不是列。並且在該表中應該是名爲studentPhoto的列。 –

+0

是啊,到底....但沒有任何聯繫,它的類型是字節...我會嘗試你的方法晚些時候,讓你知道 – johnrao07