2 years ago

#72895

test-img

Qiuyan Su

Create a FileDescriptor object on Android

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

java

android

java-native-interface

file-descriptor

parcelfiledescriptor

0 Answers

Your Answer

Accepted video resources