Android 解析服务器保存图像不工作
Parse Server Saving Images with Android Not Working
我正在尝试将图像上传到我在 AWS 和 mongoLab 上的解析服务器。但是,每当我尝试使用下面的代码添加图像时,都会出现错误,当我尝试保存没有图像的对象时,它会成功。难道我做错了什么。我试了 10 多个小时都没成功。
ParseFile image1;
Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.ph);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
image1 = new ParseFile("profilePhoto.png", byteArray);
image1.saveInBackground();
JSONArray myFriendList = new JSONArray();
myFriendList.put("xxxxxxxxxxxxxxx");
myFriendList.put("yyyyyyyyyyyyyyy");
ParseObject userSettingObj = new ParseObject("userSetting");
userSettingObj.put("profileName", profileNameField.getText().toString());
userSettingObj.put("userid", ParseUser.getCurrentUser().getObjectId());
userSettingObj.put("name", nameField.getText().toString());
userSettingObj.put("surname", surnameField.getText().toString());
userSettingObj.put("friendList", myFriendList);
userSettingObj.put("photo", image1);
userSettingObj.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
System.out.println("saved successfully");
} else {
System.out.println("error while saving");
}
}
});
试试这个然后告诉我。
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
image1 = new ParseFile("profilePhoto.png", byteArray);
image1.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
upload();
} else {
}
}
});
public void upload(){
JSONArray myFriendList = new JSONArray();
myFriendList.put("xxxxxxxxxxxxxxx");
myFriendList.put("yyyyyyyyyyyyyyy");
ParseObject userSettingObj = new ParseObject("userSetting");
userSettingObj.put("profileName", profileNameField.getText().toString());
userSettingObj.put("userid", ParseUser.getCurrentUser().getObjectId());
userSettingObj.put("name", nameField.getText().toString());
userSettingObj.put("surname", surnameField.getText().toString());
userSettingObj.put("friendList", myFriendList);
userSettingObj.put("photo", byteArray);
userSettingObj.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
System.out.println("saved successfully");
} else {
System.out.println("error while saving");
}
}
});}
经过几个小时的努力,我终于能够在 Parse DataBase 上上传 Parse 文件
要遵循的步骤:
1 - 首先编写注册查询。
2 - 成功响应注册查询后,您将获得当前用户对象 ID。
3- 然后在 Parse 数据库 "profilePictureTable" 中创建一个自定义 Class 并添加某些列 1- profilePicture。 2- 用户对象 ID。
4- 然后最后调用回调 SaveInBackground
Bitmap bitmap = BitmapFactory.decodeFile("Your File Path");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] data = stream.toByteArray();
final ParseFile file = new ParseFile("profile_pic.png", data);
file.saveInBackground();
final ParseObject profilePicture = new ParseObject("profilePictureTable");
profilePicture.put("profilePicture", file);
profilePicture.put("userObjectId",ParseUser.getCurrentUser().getObjectId());
profilePicture.saveInBackground(new SaveCallback()
{
@Override
public void done(ParseException e)
{
if (e == null)
{
Log.i("Parse", "saved successfully");
}
else
{
Log.i("Parse", "error while saving");
}
}
});
我正在尝试将图像上传到我在 AWS 和 mongoLab 上的解析服务器。但是,每当我尝试使用下面的代码添加图像时,都会出现错误,当我尝试保存没有图像的对象时,它会成功。难道我做错了什么。我试了 10 多个小时都没成功。
ParseFile image1;
Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.ph);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
image1 = new ParseFile("profilePhoto.png", byteArray);
image1.saveInBackground();
JSONArray myFriendList = new JSONArray();
myFriendList.put("xxxxxxxxxxxxxxx");
myFriendList.put("yyyyyyyyyyyyyyy");
ParseObject userSettingObj = new ParseObject("userSetting");
userSettingObj.put("profileName", profileNameField.getText().toString());
userSettingObj.put("userid", ParseUser.getCurrentUser().getObjectId());
userSettingObj.put("name", nameField.getText().toString());
userSettingObj.put("surname", surnameField.getText().toString());
userSettingObj.put("friendList", myFriendList);
userSettingObj.put("photo", image1);
userSettingObj.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
System.out.println("saved successfully");
} else {
System.out.println("error while saving");
}
}
});
试试这个然后告诉我。
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
image1 = new ParseFile("profilePhoto.png", byteArray);
image1.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
upload();
} else {
}
}
});
public void upload(){
JSONArray myFriendList = new JSONArray();
myFriendList.put("xxxxxxxxxxxxxxx");
myFriendList.put("yyyyyyyyyyyyyyy");
ParseObject userSettingObj = new ParseObject("userSetting");
userSettingObj.put("profileName", profileNameField.getText().toString());
userSettingObj.put("userid", ParseUser.getCurrentUser().getObjectId());
userSettingObj.put("name", nameField.getText().toString());
userSettingObj.put("surname", surnameField.getText().toString());
userSettingObj.put("friendList", myFriendList);
userSettingObj.put("photo", byteArray);
userSettingObj.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
System.out.println("saved successfully");
} else {
System.out.println("error while saving");
}
}
});}
经过几个小时的努力,我终于能够在 Parse DataBase 上上传 Parse 文件 要遵循的步骤: 1 - 首先编写注册查询。 2 - 成功响应注册查询后,您将获得当前用户对象 ID。 3- 然后在 Parse 数据库 "profilePictureTable" 中创建一个自定义 Class 并添加某些列 1- profilePicture。 2- 用户对象 ID。 4- 然后最后调用回调 SaveInBackground
Bitmap bitmap = BitmapFactory.decodeFile("Your File Path");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] data = stream.toByteArray();
final ParseFile file = new ParseFile("profile_pic.png", data);
file.saveInBackground();
final ParseObject profilePicture = new ParseObject("profilePictureTable");
profilePicture.put("profilePicture", file);
profilePicture.put("userObjectId",ParseUser.getCurrentUser().getObjectId());
profilePicture.saveInBackground(new SaveCallback()
{
@Override
public void done(ParseException e)
{
if (e == null)
{
Log.i("Parse", "saved successfully");
}
else
{
Log.i("Parse", "error while saving");
}
}
});