0

MemoryFile is documented as a wrapper around Android's Ashmem: https://developer.android.com/reference/android/os/MemoryFile.html

Ashmem, of course, is used to perform IPC. However in the current api of MemoryFile I don't see anything facilitating IPC.

Say in process A I want to share a Bitmap to other processes. And I want to achieve this with MemoryFile. In process A:

file = new MemoryFile("BitmapFile", 20 * 1024 * 1024);
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ipc);
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] byteArray = stream.toByteArray();
file.writeBytes(byteArray, 0, 0, byteArray.length);

After this an Ashmem object should have been created and bitmap data is written into it. Now I want to share it with Process B. What I am expecting is that there is a method in MemoryFile returning a file descriptor. The I can pass this file descriptor to Process B via Binder. Then Process B can create a MemoryFile with this file descriptor and read data. However there is no api in MemoryFile facilitating this. How can I do IPC with MemoryFile then?

darklord
  • 5,077
  • 12
  • 40
  • 65

1 Answers1

0

If you want to share an image using share memory you can use this Shared Memory library (link). I have tested the demo apps and it seems to do what you want to accomplish.

Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), mBitmapRes);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bmBytes = stream.toByteArray();
mSharedRegion.writeBytes(bmBytes, 0, 0, bmBytes.length);

More details in the Documentation