Hi
I'm not really sure whether this has been updated or not, however I have just collected all code I could find in relation to this and consolidated into one class. I'm trying to embed some js and html files into a library i'm doing. If there is a better way please advise...
Original
https://www.b4x.com/android/forum/threads/embedding-files-in-compiled-libraries.37689/#content
I'm not really sure whether this has been updated or not, however I have just collected all code I could find in relation to this and consolidated into one class. I'm trying to embed some js and html files into a library i'm doing. If there is a better way please advise...
Original
https://www.b4x.com/android/forum/threads/embedding-files-in-compiled-libraries.37689/#content
B4X:
'Class module
Sub Class_Globals
Private fromAssets As Boolean
End Sub
Public Sub Initialize (FromAssetsFile As Boolean)
fromAssets = FromAssetsFile
End Sub
Public Sub LoadFileFileFromJar(FileName As String) As InputStream
If fromAssets 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
Public Sub LoadBitmapFromJar(FileName As String) As Bitmap
Dim bmp As Bitmap
Dim In As InputStream = LoadFileFileFromJar(FileName)
bmp.Initialize2(In)
In.Close
Return bmp
End Sub
Public Sub LoadBitmapSampleFromJar(FileName As String, Width As Int, Height As Int) As Bitmap
If File.Exists(File.DirInternalCache, FileName) = False Then
Dim out As OutputStream = File.OpenOutput(File.DirInternalCache, FileName, False)
File.Copy2(LoadFileFileFromJar(FileName), out)
out.Close
End If
Return LoadBitmapSample(File.DirInternalCache, FileName, Width, Height)
End Sub
Public Sub AssetFromJar(FileName As String) As Boolean
Try
If File.Exists(File.DirInternalCache, FileName) = False Then
Dim out As OutputStream = File.OpenOutput(File.DirInternalCache, FileName, False)
File.Copy2(LoadFileFileFromJar(FileName), out)
out.Close
Return True
Else
Return True ' File OK anyway
End If
Catch
Log("Failed to copy file to Assets: " & FileName)
Return False
End Try
End Sub
Sub LoadNinePatchDrawable(FileName As String) As Object
Dim b As Bitmap = LoadBitmapFromJar(FileName)
Dim jo As JavaObject = b
Dim chunk() As Byte = jo.RunMethod("getNinePatchChunk", Null)
Dim nineDrawable As JavaObject
nineDrawable.InitializeNewInstance("android.graphics.drawable.NinePatchDrawable", Array(Null, b, chunk, Null, ""))
Return nineDrawable
End Sub