从 getter 方法获取数据而不是传入 Bundle
Get data from getter method instead of passing in a Bundle
我需要将一些位图从一个 activity 传递到另一个,并且由于 Bundle 的大小限制不允许我传递这些图像(即使使用字节数组*),我认为我可以在这些活动之间使用 getter 方法。
-但是,由于我还不是 Android (Java) 的大师,我不知道这是否会有所不同,如果有,使用时需要注意什么
字节数组确实减少了总大小(大约 60%),但仍然不够
缩小规模是一种出路,但以防万一其他解决方案有效
通过在POJO中写getter方法来传递Uri class
将您的对象保存在文件中
private void saveDataToFile() {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = getContext().openFileOutput("fileName", Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NullPointerException e) {
}
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(fileOutputStream);
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
}
try {
if (objectOutputStream != null) {
objectOutputStream.writeObject(yourObject); //which data u want to save
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (objectOutputStream != null) {
objectOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
从另一个检索对象 activity
private void getDataFromFile() {
FileInputStream fileInputStream = null;
try {
fileInputStream = getContext().openFileInput("fileName");
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(fileInputStream);
} catch (IOException |NullPointerException e) {
e.printStackTrace();
}
try {
yourObject = (ObjectClass) objectInputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
如果您想使用 getter setter,只需创建位图的 URI 并传递给 POJO class 中的 setter 方法,然后使用 [=14] 检索=] POJO的方法class.
我需要将一些位图从一个 activity 传递到另一个,并且由于 Bundle 的大小限制不允许我传递这些图像(即使使用字节数组*),我认为我可以在这些活动之间使用 getter 方法。
-但是,由于我还不是 Android (Java) 的大师,我不知道这是否会有所不同,如果有,使用时需要注意什么
字节数组确实减少了总大小(大约 60%),但仍然不够
缩小规模是一种出路,但以防万一其他解决方案有效
通过在POJO中写getter方法来传递Uri class
将您的对象保存在文件中
private void saveDataToFile() {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = getContext().openFileOutput("fileName", Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NullPointerException e) {
}
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(fileOutputStream);
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
}
try {
if (objectOutputStream != null) {
objectOutputStream.writeObject(yourObject); //which data u want to save
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (objectOutputStream != null) {
objectOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
从另一个检索对象 activity
private void getDataFromFile() {
FileInputStream fileInputStream = null;
try {
fileInputStream = getContext().openFileInput("fileName");
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(fileInputStream);
} catch (IOException |NullPointerException e) {
e.printStackTrace();
}
try {
yourObject = (ObjectClass) objectInputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
如果您想使用 getter setter,只需创建位图的 URI 并传递给 POJO class 中的 setter 方法,然后使用 [=14] 检索=] POJO的方法class.