Android Question Can I add picture in Lib?

RandomCoder

Well-Known Member
Licensed User
Longtime User
It took some searching but I knew this link was out there somewhere as I've had to do this myself in the past....
Embedding files in compiled libraries
Don't know if it works with music files though, but i guess its worth a try!
 
Upvote 0

Tayfur

Well-Known Member
Licensed User
Longtime User
It took some searching but I knew this link was out there somewhere as I've had to do this myself in the past....
Embedding files in compiled libraries
Don't know if it works with music files though, but i guess its worth a try!

I read your link.
So I cant it. I added @Erel code.

step 1.
I added your codein my test lib.
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 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
        Msgbox("dosya chahe kaydedili","")
      
        Dim out2 As OutputStream = File.OpenOutput(File.DirDefaultExternal, FileName, False)
        File.Copy2(LoadFileFileFromJar(FileName), out2)
        out2.Close
        Msgbox("dosya root kaydedili","")
      
        Return True
      Else
          Msgbox("asset : Chaste dosya var","")
        Return True    ' File OK anyway
    End If
Catch
    Log("Failed to copy file to Assets: " & FileName)
    Msgbox("Kopyalanamadı","")
    Return False
End Try
End Sub

Public Sub LoadBitmapSampleFromJar2(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
     Msgbox("dosya2 chahe kaydedili","")
     Dim out2 As OutputStream = File.OpenOutput(File.DirDefaultExternal, FileName, False)
     File.Copy2(LoadFileFileFromJar(FileName), out2)
     out2.Close
     Msgbox("dosya2 root kaydedili","")
   End If
   Return LoadBitmapSample(File.DirInternalCache, FileName, Width, Height)
End Sub

Public Sub Loader(FileName As String) As Boolean
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
        Msgbox("dosya chahe kaydedili","")
      
        Dim out2 As OutputStream = File.OpenOutput(File.DirDefaultExternal, FileName, False)
        File.Copy2(LoadFileFileFromJar(FileName), out2)
        out2.Close
        Msgbox("dosya root kaydedili","")
      
        Return True
      Else
          Msgbox("asset : Chaste dosya var","")
        File.Copy(File.DirInternalCache,"home.png",File.DirRootExternal,"home2.png")
        File.Copy(File.DirInternalCache,"home.png",File.DirDefaultExternal,"home2.png")
        Msgbox("dosya home2 root kaydedili","")
        Return True    ' File OK anyway
    End If
End Sub


Step 2 : Compile. After I insert manuelly "home.png" with using winrar
Untitled.png



Step3. Create test projecet, I added this lib (loader.jar)

I tried alot of things; So Cant load image :(:(:(:(:(

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
Private jfl As JarFileLoader
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
  
    Private ImageView1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("L1")
      If FirstTime Then
     jfl.Initialize(True) '<--- True = from assets folder, False = from jar file
   End If
   'jfl.AssetFromJar("home.png")
    jfl.Loader("home.png")
    'bmpBitmap.InitializeSample(File.DirInternalCache, sImage, iButtonSize, iButtonSize)
    Activity.SetBackgroundImage(LoadBitmap( File.DirInternalCache,"HOME.png"))
   Activity.SetBackgroundImage(jfl.LoadBitmapSampleFromJar2("home.png",100,100))  
   If File.Exists(File.DirInternalCache,"home.png") Then Log("dosya inter chc var") Else Log("dosya inter chc yok")
   If File.Exists(File.DirRootExternal,"home2.png") Then Log("dosya dir root var") Else Log("dosya dir root yok")
   If File.Exists(File.DirDefaultExternal,"home2.png") Then Log("dosya dir def ext  var") Else Log("dosya dir def ext yok")
        'File.Copy(File.DirInternalCache,"home.png",File.DirDefaultExternal,"home2.png")
        ImageView1.Bitmap=LoadBitmap(File.DirInternalCache,"home.png")
      
End Sub


Where is the wrong???
 
Upvote 0

Tayfur

Well-Known Member
Licensed User
Longtime User
It took some searching but I knew this link was out there somewhere as I've had to do this myself in the past....
Embedding files in compiled libraries
Don't know if it works with music files though, but i guess its worth a try!

Sorry.
YOur code is correct. Icant see this line.
Only change it, False and code works.

B4X:
jfl.Initialize(False) '<--- True = from assets folder, False = from jar file

thank you for helps...
 
Upvote 0
Top