Android 解析文件查询不工作
我正试图通过查询从Parse获取一个文件。Parse对于如何从云中获取文件非常模糊,但我已经设法创建了一个基本查询来尝试获取文件Android 解析文件查询不工作,android,parse-platform,imageview,Android,Parse Platform,Imageview,我正试图通过查询从Parse获取一个文件。Parse对于如何从云中获取文件非常模糊,但我已经设法创建了一个基本查询来尝试获取文件 ParseQuery query = ParseUser.getQuery(); query.getInBackground(objects.get(i).getObjectId(), new GetCallback<ParseObject>() { public v
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();
}
}
});
}
});
需要通过此方法获取文件的帮助。或者有没有更简单的方法从解析云获取文件?非常感谢您的帮助。您的代码看起来不错,只需检查当前行的
图片列中是否有文件引用即可。如果列为空,那么在将其作为ParseFile
读取时,您显然会得到一个空引用。根据我的经验,第行上的空指针异常包含以下内容:
ParseFile fileObject = (ParseFile) object.get("picture");
告诉我在尝试访问get()
方法期间,对象
为空。这告诉我,您的查询要么格式不正确,要么服务器上没有匹配的对象,更可能是第二个
我没有关于解析云数据结构的任何信息,所以让我给你举个例子
如果您有一个普通的老用户,他的Parse ObjectId是xyz123ab
,并且在键/列profilePicture
下有一个图片文件。您的用户包含包含图片数据的解析文件,或[用户[文件[图片数据]]
以下是如何在代码中检索上述内容:
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
}
}
});
此外,请确保数据已正确加载到服务器中。不幸的是,在web客户机上进行解析时,很难确定这一点。正确上传图像与此类似,但相反,需要另一天的答复
请检查您的代码以获取objectId,如下所示:
objects.get(i).getObjectId()
确定您是否确实在访问您认为是的objectId,可以使用日志、toast或调试断点。那可能是因为一切都开始崩溃了
// ... 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...
objects.get(i).getObjectId()