Android - 使用Bitmap将对象保存到文件

时间:2012-03-10 22:28:53

标签: android file serialization bitmap

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;
}

这是我创建“挑战”模型的代码。所有挑战都有一个位图的图像,现在我想将ArrayList保存到文件中以便在本地保存它们,这就是我保存的方式:

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);
    }           
}

这会抛出一个异常,告诉我位图不是可序列化的,我怎么能用位图保存这些对象? 非常感谢答案,谢谢:)。

1 个答案:

答案 0 :(得分:0)

我只是将图像保存到SD卡。