当前位置: 动力学知识库 > 问答 > 编程问答 >

Android - Save object with Bitmap to file

问题描述:

public class Challenge implements Comparable<Challenge>, Serializable {

private static final long serialVersionUID = 6970603871560357536L;

/* Strings to use for database and server communication */

public static final String CHALLENGE_ID = "cid";

public static final String NAME = "name";

public static final String DESCRIPTION = "description";

public static final String TYPE = "type";

public static final String START_DATE = "start_date";

public static final String END_DATE = "end_date";

public static final String GOAL = "goal";

public static final String GROUP_CHALLENGE = "group_challenge";

public static final String ACTIVITY_NAME = "activity_name";

public static final String STATUS = "status";

public static final String USER_POSITION = "user_position";

public static final String USER_VALUE = "user_value";

public static final String NUMBER_OF_PARTICIPANTS = "number_of_participants";

public static final String UNIT_NAME = "unit_name";

public static final String IMAGE_BASE64 = "image_base64";

public static final String USER_GROUPS = "user_groups";

public static final String USER_STATUS = "user_status";

public static final String HAS_MAP = "has_map";

public static final String MAP_ID = "map_id";

public static final int ACTION_ACCEPT = 1;

public static final int ACTION_CANCEL = 2;

public static final int STATUS_PENDING = 0;

public static final int STATUS_ACCEPTED = 1;

public static final int STATUS_DECLINED = 2;

public static final int STATUS_FINISHED = 3;

public static final int STATUS_NOT_FINISHED = 4;

public static final int CHALLENGE_STATUS_ACTIVE = 0;

public static final int CHALLENGE_STATUS_CANCELED = 1;

public static final int CHALLENGE_STATUS_FINISHED = 2;

public static final int CHALLENGE_TYPE_FIRST_TO_GOAL = 0;

public static final int CHALLENGE_TYPE_UNLIMITED_GOAL = 1;

/* Fields */

private int cid;

private String name;

private String description;

private int type;

private Date startDate;

private Date endDate;

private double goal;

private boolean groupChallenge;

private String acitivityName;

private int status;

private int userPosition;

private double userValue;

private int numberOfParticipants;

private String unitName;

private Bitmap image;

private int userStatus;

private boolean hasMap;

private int mapId;

private ArrayList<Group> userGroups;

/* Getters and setters */

public int getCid() {

return cid;

}

public void setCid(int cid) {

this.cid = cid;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

public int getType() {

return type;

}

public void setType(int type) {

this.type = type;

}

public Date getStartDate() {

return startDate;

}

public void setStartDate(Date startDate) {

this.startDate = startDate;

}

public Date getEndDate() {

return endDate;

}

public void setEndDate(Date endDate) {

this.endDate = endDate;

}

public double getGoal() {

return goal;

}

public void setGoal(double goal) {

this.goal = goal;

}

public String getAcitivityName() {

return acitivityName;

}

public void setAcitivityName(String acitivityName) {

this.acitivityName = acitivityName;

}

public int getStatus() {

return status;

}

public void setStatus(int status) {

this.status = status;

}

public int getUserPosition() {

return userPosition;

}

public void setUserPosition(int userPosition) {

this.userPosition = userPosition;

}

public double getUserValue() {

return userValue;

}

public void setUserValue(double userValue) {

this.userValue = userValue;

}

public int getNumberOfParticipants() {

return numberOfParticipants;

}

public void setNumberOfParticipants(int numberOfParticipants) {

this.numberOfParticipants = numberOfParticipants;

}

public String getUnitName() {

return unitName;

}

public void setUnitName(String unitName) {

this.unitName = unitName;

}

public Bitmap getImage() {

return image;

}

public void setImage(Bitmap image) {

this.image = image;

}

This is my code for a model I created "Challenge". All challenges have an image which is a bitmap, and now I want to save ArrayList to a file in order to save them locally, this is how I save it:

public boolean saveOpenChallenges(List<Challenge> challenges) {

FileOutputStream fos = null;

ObjectOutputStream out = null;

try {

File new_file = new File(context.getFilesDir(), CHALLENGES_FILE_NAME);

new_file.createNewFile();

fos = new FileOutputStream(new_file);

out = new ObjectOutputStream(fos);

out.writeObject(challenges);

out.close();

Log.d("daim", "saving challenges to file!");

return true;

} catch (Exception e) {

Log.e("bajs", "" + e.getMessage());

return false;

} finally {

close(fos);

}

}

This will cast an exception telling me that bitmap is not serializable or something, how I can achieve saving these objects with the bitmap on?

Really appreciate an answer, thanks :)

网友答案:

I just saved the image to the SD card instead.

分享给朋友:
您可能感兴趣的文章:
随机阅读: