2

I want to convert the filedescriptor (int) obtained from native to filedescriptor (Java object)

//native

int fd = open(......);

Why doesn't example 1 work? Maybe in theory?

//eg 1:

FileDescriptor mFd = ParcelFileDescriptor.adoptFd(fd).getFileDescriptor();

//eg 2:

FileDescriptor mFd = new FileDescriptor()
Method setFd = FileDescriptor.class.getDeclaredMethod("setInt$", int.class);
setFd.setAccessible(true);
setFd.invoke(mFd, fd);

Microsoft:

Take ownership of a raw native fd in to a new ParcelFileDescriptor.

C#

[Android.Runtime.Register("adoptFd", "(I)Landroid/os/ParcelFileDescriptor;", "")]

public static Android.OS.ParcelFileDescriptor? AdoptFd (int fd);

Parameters fd Int32 The native fd that the ParcelFileDescriptor should adopt.

Returns

ParcelFileDescriptor

Returns a new ParcelFileDescriptor holding a FileDescriptor for the given fd.

Attributes RegisterAttribute

https://learn.microsoft.com/zh-cn/dotnet/api/android.os.parcelfiledescriptor.adoptfd?view=xamarin-android-sdk-12

拉拉姬
  • 140
  • 1
  • 8
  • 1
    1 wouldn't work because a ParcelFileDescriptor is not a subclass of FileDescriptor. The second I wouldn't use at all- its relying on internal implementation details of the FileDescriptor object, which could change at any time. – Gabe Sechan Jan 20 '22 at 04:42
  • @Gabe Sechan https://learn.microsoft.com/zh-cn/dotnet/api/android.os.parcelfiledescriptor.adoptfd?view=xamarin-android-sdk-12 – 拉拉姬 Jan 20 '22 at 05:37

0 Answers0