Android - 将带有位图的对象保存到文件
Posted
技术标签:
【中文标题】Android - 将带有位图的对象保存到文件【英文标题】:Android - Save object with Bitmap to file 【发布时间】:2012-03-27 21:20:12 【问题描述】: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);
这将抛出一个异常,告诉我位图不可序列化之类的,我如何才能在打开位图的情况下保存这些对象? 非常感谢您的回答,谢谢:)
【问题讨论】:
您遇到了什么异常?你能在这里发布一些错误日志吗... 没关系,我只是将位图保存到 SD 卡 :) 【参考方案1】:我只是将图像保存到 SD 卡中。
【讨论】:
以上是关于Android - 将带有位图的对象保存到文件的主要内容,如果未能解决你的问题,请参考以下文章