B4J Question Load Image or File from Jar

margret

Well-Known Member
Licensed User
Longtime User
@Erel

You provided the code below for B4A, to load an image from the library jar file instead of the Assets folder. It will not work in B4J. Can you povide the changes or new code that will? Also, it would be nice to load a data file or list this way as well. Thanks for any help:

B4X:
Private Sub LoadFileFromJar(FileName As String) As InputStream
   'If fromAssets Then Return File.OpenInput(File.DirAssets, FileName)
   If File.Exists(File.DirAssets, "ftypes") Then Return File.OpenInput(File.DirAssets, FileName)
   Dim r As Reflector
   r.Target = Me
   r.Target = r.RunMethod("getClass")
   Dim In As InputStream = r.RunMethod2("getResourceAsStream", "/" &  FileName, "java.lang.String")
   If In.IsInitialized = False Then
      LogColor("Cannot find file: " & FileName, Colors.Red)
   End If
   Return In
End Sub
Private Sub LBFJ(FileName As String) As Bitmap
   Dim bmp As Bitmap
   Dim In As InputStream = LoadFileFromJar(FileName)
   bmp.Initialize2(In)
   In.Close
   Return bmp
End Sub
 

margret

Well-Known Member
Licensed User
Longtime User
I got rid of File.Exists but the other code still dos not work. It will run in development with the files in the assets but once you have compiled to lib and add the images,it give the error file not found. I have done this same thing in B4A in my new FileSelect lib which includes a lot of images and it works great. Any idea why it does not work in B4J?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that you should only use Process_Globals and Class_Globals to declare variables and for assignments of primitive values. Your current code can cause all kinds of problems.

It is simpler to embed files in Jars in B4J. All you need to do is add a Files (not files) folder to the jar with the files. You can then load them like you load any other "asset" file. No need to write any special code.

The compiler will show a warning on this line. You can add 'ignore comment to remove it.
 
Upvote 0
Top