Reputation: 821
I am trying to upload an image to my parse server on AWS and mongoLab. However whenever I try to add the image with the code below, I got an error, when I try to save the object without the image, it succeeds. Am I doing something wrong. I am trying for more than 10 hours and could not make it work.
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");
}
}
});
Upvotes: 0
Views: 624
Reputation: 21
Finally after few hours effort I am able to upload a Parse file on Parse DataBase Steps To Follow: 1 - Write a SignUp Query First. 2 - On Successful response of SignUp Query you will get a Current User Object Id. 3- Then Create One Custom Class in Parse database "profilePictureTable" and add Certain Columns 1- profilePicture. 2- userObjectId. 4- Then Finally Call Callback 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");
}
}
});
Upvotes: 1
Reputation: 8281
Try this and let me know.
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");
}
}
});}
Upvotes: 2