我这一天都被困住了。我已经尝试了所有其他方法来实现这一目标。以下方法几乎完成了工作,但它为变量文件提供了空指针异常。
以下代码介绍了如何保存数据
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();
}
}
});
答案 0 :(得分:1)
我不知道为什么你的方法不起作用,但我会告诉你我从解析中获取照片的方法。 首先,确保您已成功保存图像 - 为此,在parse.com面板上转到您的应用程序,打开Core选项卡,并检查您的Student表是否有名为studentPhoto的列,并且应该有指向该列的链接相片。 我从解析中获取图像的方法是使用Picasso库。简单地说,从解析你应该只获得照片的链接,而不是整个文件,然后使用毕加索下载该照片。 要获取文件链接,请执行以下操作:
$this->container['userService'] = function ($c) {
return new UserService($c['profileService']);
};
$this->container['profileService'] = function ($c) {
return new ProfileService($c['userService']);
};
然后使用Picasso设置该图像:
ParseObject object = ... // your student object
try {
object.fetchIfNeeded();
} catch (ParseException e) {
e.printStackTrace();
}
ParseFile avatar = object.getParseFile("studentPhoto");
String avatarUrl = avatar.getUrl();
Picasso网站了解更多信息:http://square.github.io/picasso/