Java Question Creating a file for B4A use

barx

Well-Known Member
Licensed User
Longtime User
I've not worked with files much in B4A and I'm assuming that the answer I'm looking for will require some sort of InputStream / OutputStream work.

I'm adding to the Wearable DataLayer lib the methods to extract a bitmap or file held within an asset.

For Bitmap I think I have it covered by following Googles docs. This the code for that

B4X:
    public Bitmap GetBitmapFromAsset(Asset asset) {
        if (asset == null) {
            throw new IllegalArgumentException("Asset cannot be null");
        }
       
        InputStream assetInputStream = Wearable.DataApi.getFdForAsset(mGoogleApiClient, asset).await().getInputStream();
       
        if (assetInputStream == null) {
            return null;
        }
        return BitmapFactory.decodeStream(assetInputStream);
    }

but I' a little stumped for how to go about it for files. Do I just return the InputStream? Or do I get the user to pass a TargetDir and TargetFilename and save the file to the filesystem. If the latter, how on earth do I go about that? I was hoping there was something similar to the Bitmap solution like new FileFromStream but I cannot find such thing.

Cheers
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Asset files are special and needs to be handled in a unique way (like you did).

For the assets files you can create a single method that returns an InputStream. Developers can use this input stream to do what ever then need, including to create a bitmap with Bitmap.Initialize2.

For other files you don't need to do anything special. Just pass the correct path for the directory component.
 
Top