解析文件查询不工作

Parse File Query Not Working

我正在尝试通过查询从 Parse 获取文件。 Parse 对如何从云端获取文件非常模糊,但我设法创建了一个基本查询来尝试获取文件

ParseQuery query = ParseUser.getQuery();
                    query.getInBackground(objects.get(i).getObjectId(), new GetCallback<ParseObject>() {

                        public void done(ParseObject object, ParseException e) {

                            ParseFile fileObject = (ParseFile) object.get("picture");
                            fileObject.getDataInBackground(new GetDataCallback() {

                                public void done(byte[] data, ParseException e) {
                                    if (e == null) {
                                        Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);

                                        ImageView image = (ImageView) findViewById(R.id.profilePicture);

                                        image.setImageBitmap(bmp);

                                    } else {
                                        Toast.makeText(getApplicationContext(), "Image Not Found", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                    });

上面的代码给我一个NullPointerException就行了:

ParseFile fileObject = (ParseFile) object.get("picture");

需要帮助通过此方法获取文件。或者是否有更简单的方法从 Parse Cloud 获取文件?感谢所有帮助。

您的代码看起来不错,您只需检查当前行的 picture 列中是否有文件引用。如果该列为空,那么在将其作为 ParseFile.

读出时显然会得到空引用

根据我的经验,空指针异常包含以下内容:

ParseFile fileObject = (ParseFile) object.get("picture");

告诉我在尝试访问 get() 方法期间 object 为空。这告诉我您的查询格式不正确或在服务器上没有匹配的对象,更有可能是第二个。

我没有关于你的 Parse 云的数据结构的任何信息,所以让我给你举个例子。

如果你有一个普通的老用户,他的Parse ObjectId是xyz123ab并且他在key/columnprofilePicture下有一个图片文件。您有一个 User 包含一个包含图片数据的 ParseFile,或 [User [File [PictureData]]]。

以下是在代码中检索上述内容的方法:

String objectIdOfUser = "xyz123ab";
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.getInBackground(objectIdOfUser, new GetCallback<ParseUser>() {
  @Override
  public void done(ParseUser parseUser, ParseException e) {
    if (e == null) {
      ParseFile picture = parseUser.getParseFile("profilePicture");
      if (picture == null) {
        // no parseFile in column "profilePicture" in this user.
        return; // there will be no data to retrieve
      }

      picture.getDataInBackground(new GetDataCallback() {
        @Override
        public void done(byte[] data, ParseException e) {
          if (e == null) {
            if (data.length == 0) {
              // data found, but nothing to extract. bad image or upload?
              return; // failing out
            }

            // SUCCESS
            // convert data and display in an imageview
          } else {
            // ParseFile contained no data. data not added to ParseFile?
          }
        }
      });
    } else {
      // no users had this objectId
    }
  }
});

如果您不使用 ParseUser,请在下面将 ParseUser 更改为 ParseObject。不要忘记将字符串 "ObjectName" 更改为您的对象。

// ... etc
ParseQuery<ParseObject> query = ParseObject.getQuery("ObjectName");
query.getInBackground(objectIdOfObject, new GetCallback<ParseObject>() {
  @Override
  public void done(ParseObject object, ParseException e) {
    if (e == null) {
      ParseFile picture = object.getParseFile("profilePicture");
      // etc...

此外,请确保您的数据已正确加载到服务器中。不幸的是,在 Parse 的 Web 客户端上确定这可能很棘手。正确上传图片与此类似但相反,改天再回答。

请查看您的代码以获取 objectId,如下所示:

objects.get(i).getObjectId()

确定您是否确实在访问您认为是的 objectId,可能使用日志或 toast 或调试断点。那可能是一切都开始崩溃了。