Java Question Embedding resources inside the Jar file

Erel

Administrator
Staff member
Licensed User
Starting from Basic4android v2.00 it is possible to embed resource files inside the library jar. During compilation the resource files will be copied to the APK file automatically.

Note that you cannot use the standard Android resources mechanism. Android SDK currently doesn't support it. This means that XML layouts cannot be added this way.

The first step is to add the resource file to the Eclipse project under the Package node (the image name is droid.png):



To access the embedded resource you should use the following code:
B4X:
this.getClass().getResourceAsStream("droid.png")
For example:
B4X:
public Bitmap GetImage() {
      BitmapWrapper bw = new BitmapWrapper();
      bw.Initialize2(this.getClass().getResourceAsStream("droid.png"));
      return bw.getObject();
   }
 

Johan Schoeman

Expert
Licensed User
Starting from Basic4android v2.00 it is possible to embed resource files inside the library jar. During compilation the resource files will be copied to the APK file automatically.

Note that you cannot use the standard Android resources mechanism. Android SDK currently doesn't support it. This means that XML layouts cannot be added this way.

The first step is to add the resource file to the Eclipse project under the Package node (the image name is droid.png):



To access the embedded resource you should use the following code:
B4X:
this.getClass().getResourceAsStream("droid.png")
For example:
B4X:
public Bitmap GetImage() {
      BitmapWrapper bw = new BitmapWrapper();
      bw.Initialize2(this.getClass().getResourceAsStream("droid.png"));
      return bw.getObject();
   }
Erel, does this also apply to Simple Library Compiler? Can I embed a .ogg file in the jar file via SLC? If so, how will the call to read/play the .ogg file be in the Java method?
 

DonManfred

Expert
Licensed User
Can I embed a .ogg file in the jar file via SLC?
from the description i would say yes.
If so, how will the call to read/play the .ogg file be in the Java method?
if the ogg-player have a method to read from an inputstream then i guess something like
B4X:
oggplayer.playstream(this.getClass().getResourceAsStream("droid.ogg"));
 

mshafiee110

Active Member
Licensed User
I tested it.
but this error eccure

B4X:
** Activity (main) Create, isFirst = true **
main_activity_create (java line: 341)
java.lang.RuntimeException: Error loading bitmap.
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:521)
    at anywheresoftware.b4a.find.GetImage(find.java:24)
    at aaa.bbb.main._activity_create(main.java:341)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at aaa.bbb.main.afterFirstLayout(main.java:104)
    at aaa.bbb.main.access$000(main.java:19)
    at aaa.bbb.main$WaitForLayout.run(main.java:82)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5021)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Error loading bitmap.
** Activity (main) Resume **
 
Top