Android-将带有位图的对象保存到文件 public类质询实现了可比较、可序列化的{ 私有静态最终长serialVersionUID=6970603871560357536L; /*用于数据库和服务器通信的字符串*/ 公共静态最终字符串质询\u ID=“cid”; 公共静态最终字符串NAME=“NAME”; 公共静态最终字符串DESCRIPTION=“DESCRIPTION”; 公共静态最终字符串TYPE=“TYPE”; 公共静态最终字符串START\u DATE=“START\u DATE”; 公共静态最终字符串END\u DATE=“END\u DATE”; 公共静态最终字符串GOAL=“GOAL”; 公共静态最终字符串GROUP\u CHALLENGE=“GROUP\u CHALLENGE”; 公共静态最终字符串ACTIVITY\u NAME=“ACTIVITY\u NAME”; 公共静态最终字符串STATUS=“STATUS”; 公共静态最终字符串USER\u POSITION=“USER\u POSITION”; 公共静态最终字符串USER\u VALUE=“USER\u VALUE”; 公共静态最终字符串NUMBER\u OF_PARTICIPANTS=“NUMBER\u OF_PARTICIPANTS”; 公共静态最终字符串UNIT\u NAME=“UNIT\u NAME”; 公共静态最终字符串IMAGE\u BASE64=“IMAGE\u BASE64”; 公共静态最终字符串USER\u GROUPS=“USER\u GROUPS”; 公共静态最终字符串USER\u STATUS=“USER\u STATUS”; 公共静态最终字符串HAS\u MAP=“HAS\u MAP”; 公共静态最终字符串MAP\u ID=“MAP\u ID”; 公共静态最终int动作_ACCEPT=1; 公共静态最终int动作\u取消=2; 公共静态最终int状态_PENDING=0; 公共静态最终int状态_接受=1; 公共静态最终int状态_=2; 公共静态最终int状态_FINISHED=3; 公共静态最终整数状态\u未\u完成=4; 公共静态最终整数挑战\状态\活动=0; 公共静态最终整数挑战\状态\取消=1; 公共静态最终整数挑战\状态\完成=2; 公共静态最终整数挑战\u类型\u第一个\u到\u目标=0; 公共静态最终整数挑战\类型\无限\目标=1; /*田地*/ 私家侦探; 私有字符串名称; 私有字符串描述; 私有int型; 私人日期开始日期; 私人日期结束日期; 私人双重目标; 私人布尔群挑战; 私有字符串acitivityName; 私人身份; 私人职位; 私有双用户值; 私人参与; 私有字符串单元名; 私有位图图像; 私有用户身份; 私有布尔hasMap; 私有int mapId; 私有ArrayList用户组; /*接球手和接球手*/ 公共int getCid(){ 返回cid; } 公共无效设置cid(内部cid){ this.cid=cid; } 公共字符串getName(){ 返回名称; } 公共void集合名(字符串名){ this.name=名称; } 公共字符串getDescription(){ 返回说明; } 公共void集合描述(字符串描述){ this.description=描述; } 公共int getType(){ 返回类型; } 公共void集合类型(int类型){ this.type=type; } 公共日期getStartDate(){ 返回起始日期; } 公共作废设置开始日期(日期开始日期){ this.startDate=startDate; } 公共日期getEndDate(){ 返回结束日期; } 公共无效设置结束日期(日期结束日期){ this.endDate=endDate; } 公共双getGoal(){ 回归目标; } 公共目标(双重目标){ 这个。目标=目标; } 公共字符串getActivityName(){ 返回acitivityName; } public void setActivityName(字符串acitivityName){ this.acitivityName=acitivityName; } public int getStatus(){ 返回状态; } 公共无效设置状态(int状态){ 这个状态=状态; } public int getUserPosition(){ 返回用户位置; } public void setUserPosition(int userPosition){ this.userPosition=userPosition; } 公共双getUserValue(){ 返回userValue; } public void setUserValue(双userValue){ this.userValue=userValue; } public int getNumberOfParticipants(){ 返回参与人数; } 公共无效集合numberOfParticipants(int numberOfParticipants){ this.numberOfParticipants=numberOfParticipants; } 公共字符串getUnitName(){ 返回unitName; } public void setUnitName(字符串unitName){ this.unitName=unitName; } 公共位图getImage(){ 返回图像; } 公共void setImage(位图图像){ 这个图像=图像; }
这是我创建的“挑战”模型的代码。所有挑战都有一个位图图像,现在我想将ArrayList保存到一个文件中,以便将其保存到本地,我的保存方式如下:Android-将带有位图的对象保存到文件 public类质询实现了可比较、可序列化的{ 私有静态最终长serialVersionUID=6970603871560357536L; /*用于数据库和服务器通信的字符串*/ 公共静态最终字符串质询\u ID=“cid”; 公共静态最终字符串NAME=“NAME”; 公共静态最终字符串DESCRIPTION=“DESCRIPTION”; 公共静态最终字符串TYPE=“TYPE”; 公共静态最终字符串START\u DATE=“START\u DATE”; 公共静态最终字符串END\u DATE=“END\u DATE”; 公共静态最终字符串GOAL=“GOAL”; 公共静态最终字符串GROUP\u CHALLENGE=“GROUP\u CHALLENGE”; 公共静态最终字符串ACTIVITY\u NAME=“ACTIVITY\u NAME”; 公共静态最终字符串STATUS=“STATUS”; 公共静态最终字符串USER\u POSITION=“USER\u POSITION”; 公共静态最终字符串USER\u VALUE=“USER\u VALUE”; 公共静态最终字符串NUMBER\u OF_PARTICIPANTS=“NUMBER\u OF_PARTICIPANTS”; 公共静态最终字符串UNIT\u NAME=“UNIT\u NAME”; 公共静态最终字符串IMAGE\u BASE64=“IMAGE\u BASE64”; 公共静态最终字符串USER\u GROUPS=“USER\u GROUPS”; 公共静态最终字符串USER\u STATUS=“USER\u STATUS”; 公共静态最终字符串HAS\u MAP=“HAS\u MAP”; 公共静态最终字符串MAP\u ID=“MAP\u ID”; 公共静态最终int动作_ACCEPT=1; 公共静态最终int动作\u取消=2; 公共静态最终int状态_PENDING=0; 公共静态最终int状态_接受=1; 公共静态最终int状态_=2; 公共静态最终int状态_FINISHED=3; 公共静态最终整数状态\u未\u完成=4; 公共静态最终整数挑战\状态\活动=0; 公共静态最终整数挑战\状态\取消=1; 公共静态最终整数挑战\状态\完成=2; 公共静态最终整数挑战\u类型\u第一个\u到\u目标=0; 公共静态最终整数挑战\类型\无限\目标=1; /*田地*/ 私家侦探; 私有字符串名称; 私有字符串描述; 私有int型; 私人日期开始日期; 私人日期结束日期; 私人双重目标; 私人布尔群挑战; 私有字符串acitivityName; 私人身份; 私人职位; 私有双用户值; 私人参与; 私有字符串单元名; 私有位图图像; 私有用户身份; 私有布尔hasMap; 私有int mapId; 私有ArrayList用户组; /*接球手和接球手*/ 公共int getCid(){ 返回cid; } 公共无效设置cid(内部cid){ this.cid=cid; } 公共字符串getName(){ 返回名称; } 公共void集合名(字符串名){ this.name=名称; } 公共字符串getDescription(){ 返回说明; } 公共void集合描述(字符串描述){ this.description=描述; } 公共int getType(){ 返回类型; } 公共void集合类型(int类型){ this.type=type; } 公共日期getStartDate(){ 返回起始日期; } 公共作废设置开始日期(日期开始日期){ this.startDate=startDate; } 公共日期getEndDate(){ 返回结束日期; } 公共无效设置结束日期(日期结束日期){ this.endDate=endDate; } 公共双getGoal(){ 回归目标; } 公共目标(双重目标){ 这个。目标=目标; } 公共字符串getActivityName(){ 返回acitivityName; } public void setActivityName(字符串acitivityName){ this.acitivityName=acitivityName; } public int getStatus(){ 返回状态; } 公共无效设置状态(int状态){ 这个状态=状态; } public int getUserPosition(){ 返回用户位置; } public void setUserPosition(int userPosition){ this.userPosition=userPosition; } 公共双getUserValue(){ 返回userValue; } public void setUserValue(双userValue){ this.userValue=userValue; } public int getNumberOfParticipants(){ 返回参与人数; } 公共无效集合numberOfParticipants(int numberOfParticipants){ this.numberOfParticipants=numberOfParticipants; } 公共字符串getUnitName(){ 返回unitName; } public void setUnitName(字符串unitName){ this.unitName=unitName; } 公共位图getImage(){ 返回图像; } 公共void setImage(位图图像){ 这个图像=图像; },android,file,serialization,bitmap,Android,File,Serialization,Bitmap,这是我创建的“挑战”模型的代码。所有挑战都有一个位图图像,现在我想将ArrayList保存到一个文件中,以便将其保存到本地,我的保存方式如下: public class Challenge implements Comparable<Challenge>, Serializable { private static final long serialVersionUID = 6970603871560357536L; /* Strings to use for database a
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;
}
public boolean saveOpenChallenges(列出挑战){
FileOutputStream=null;
ObjectOutputStream out=null;
试一试{
File new\u File=新文件(context.getFilesDir(),挑战\u文件\u名称);
new_file.createNewFile();
fos=新文件输出流(新文件);
out=新对象输出流(fos);
out.writeObject(挑战);
out.close();
Log.d(“daim”,“将挑战保存到文件!”);
返回true;
}捕获(例外e){
Log.e(“bajs”,“e.getMessage());
返回false;
}最后{
关闭(fos);
}
}
这将引发一个异常,告诉我位图不可序列化或其他什么,如何在打开位图的情况下保存这些对象?
非常感谢您的回答,谢谢:)我刚刚将图像保存到SD卡中。您遇到了什么异常情况?请在此处发布一些错误日志……不管怎样,我只是将位图保存到SD卡中:)
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);
}
}