项目场景:
需要将Bitmap在Activity和SurfaceView之间进行传递,但是试了很多种方法都不行,不是因为bitmap过大,就是报空指针错误。
最后使用Uri完美地解决了需求!
解决方案:
在SurfaceView中发送bitmap
private Bitmap bitmap;
private Uri uri;
...
//照片需要保存的路径
String filePath = "...";
File file = new File(filePath);
//判断该文件路径是否已经存在
if (!file.exists()){
file.createNewFile();
}
bos = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPG, 100, bos);
uri=Uri.fromFile(file);
Intent intent=new Intent(getContext(),MainActivity.class);
intent.setData(uri);
getContext().startActivity(intent);
在Activity中接收bitmap
private ImageView bitmapIV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Intent intent=getIntent();
Uri bitmapData=intent.getData();
bitmapIV.setImageURI(bitmapData);
}