将位图从片段共享到活动 片段中的代码:这里将位图转换为ByteArrayoutputStream并将其写入FileOutputStream
GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap snapshot) {
mapBitmap = snapshot;
try {
String fileName = "mapImage";
FileOutputStream fileOutputStream = null;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
mapBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
fileOutputStream = getActivity().openFileOutput(fileName, Context.MODE_PRIVATE);
fileOutputStream.write(bytes.toByteArray());
fileOutputStream.flush();
fileOutputStream.close();
System.gc();
} catch (Exception e) {
e.printStackTrace();
}
}
};
googleMap.snapshot(callback);
活动中的代码:从文件中检索ByteArrayoutputStream
try {
mapBitmap = BitmapFactory.decodeStream(this
.openFileInput("mapImage"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
问题:有时我正在变老的位图。表示In Fragment中传递了位图,而In Activity中检索了相同的位图。当我从fragment中传递了第三张图片时,In Activity中收到了第二张图片。