我需要将一些位图从一个 Activity 传递到另一个 Activity ,并且由于 Bundle 的大小限制不允许我传递这些图像(即使使用字节数组*),我认为我可以在这些 Activity 之间使用 getter 方法。
-但是,由于我仍然不是 Android (Java) 的高手,我不知道这是否会产生任何影响,如果有的话,使用它时我应该注意什么。
字节数组确实减少了总大小(约 60%),但仍然不够
缩小规模是一种出路,但以防万一其他解决方案有效
最佳答案
将对象保存在文件中
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();
}
}
关于java - 从 getter 方法获取数据而不是传入 Bundle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45734903/