Java Question Using FileDescriptor

barx

Well-Known Member
Licensed User
Longtime User
Hi everyone.

This is likely a question for @Erel but it is obviously open to the forces.

In a library I am currently writing, I need to use a file descriptor. I know I have seen file descriptors used in another library. I spotted it a few months ago whilst looking at existing libs code. I cannot for the life of me find the lib i saw it in so I can look at the code again as an example.

Does anything spring to mind....

Thanks
 

barx

Well-Known Member
Licensed User
Longtime User
What exactly do you need to do?

I am creating wrappers for the Wearable stuff. One of the objects to be wrapped is the 'Wearable Asset' which is used to sync files larger than 100kb. There are numerous ways to create an Asset as seen HERE.

The method I need to FD for is createFromFd. I want to keep the b4a sub intuitive, so it will be - CreateFromFile(Path As String, Filename As String).
I could have possibly figured it out by now, but you know what it is like when you are 100% sure you have seen the exact code you need and you go on a hunt.......lol
 

barx

Well-Known Member
Licensed User
Longtime User
Does this look like it could work, or am I barking up the wrong tree.

Not tried it yet as there is other code that needs writing first...

B4X:
    /**
    * Creates an Asset to use from a File
    */
    public Asset CreateFromFile(String Dir, String Filename) throws FileNotFoundException {
        File mFile = new File(Dir, Filename);
        ParcelFileDescriptor fd = new ParcelFileDescriptor(ParcelFileDescriptor.open(mFile, ParcelFileDescriptor.MODE_READ_WRITE));
      
        return Asset.createFromFd(fd);
    }
}
 

barx

Well-Known Member
Licensed User
Longtime User
I never thought of that to be honest. I wass just going forward trying to wrap each function. I guess both these methods would accomplish the same goal from the same b4a method.

So, something like this?

B4X:
    public Asset CreateFromFile(String Dir, String Filename) throws FileNotFoundException {
        File mFile = new File(Dir, Filename);
       
        return Asset.createFromUri(Uri.fromFile(mFile));
    }
 
Top