Android Question object/res image folder question

AaronF88

Member
Licensed User
Longtime User
I'm having some trouble getting a bitmapdrawable loaded into an imageview.

Code which doesn't work
http://pastebin.com/ty7NubBp

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
Dim p As Panel
Dim androidresources1 As AndroidResources
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("Layout1")
p.Initialize("me")
Activity.AddView(p, 0dip,0dip, 100%x, 100%y)
p.Color = Colors.White
Dim iv As ImageView
Dim bd As BitmapDrawable
iv.Initialize("iv")
bd = androidresources1.GetAndroidDrawable("ic_action_email")
iv.Bitmap = bd.Bitmap
p.AddView(iv, 16dip, 12dip, 32dip, 32dip)
End Sub


Code which works
http://pastebin.com/Ja69wyCQ

B4X:
Sub Globals
        'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.
Dim p As Panel
Dim androidresources1 As AndroidResources
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("Layout1")
p.Initialize("me")
Activity.AddView(p, 0dip,0dip, 100%x, 100%y)
p.Color = Colors.White
Dim iv As ImageView
iv.Initialize("iv")
iv.Bitmap = LoadBitmap(File.DirAssets, "ic_action_email.png")
p.AddView(iv, 16dip, 12dip, 32dip, 32dip)
End Sub

What I'm trying to do is load the bitmap from the objects/res folder.

I can get the Bitmap to load without using an Imageview very easily with

Dim BitmapDrawable2 As BitmapDrawable
BitmapDrawable2 = AndroidResources2.GetApplicationDrawable("topicon")
Icon1.Bitmap = BitmapDrawable2.Bitmap

And even have managed to load it with designer. However this code just doesnt seem to want to work through any variations I've tried.

If you could point me in the direction for more information (i've tried search but im not entirely sure im searching the correct terms) I would be forever in your debt

Regards

Aaron
 

AaronF88

Member
Licensed User
Longtime User
Error message from the incorrect code
B4X:
** Activity (main) Create, isFirst = true **
java.lang.RuntimeException: Object should first be initialized (BitmapDrawable).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:46)
    at anywheresoftware.b4a.objects.drawable.BitmapDrawable.getBitmap(BitmapDrawable.java:39)
    at b4a.example.main._activity_create(main.java:293)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
    at b4a.example.main.afterFirstLayout(main.java:98)
    at b4a.example.main.access$100(main.java:16)
    at b4a.example.main$WaitForLayout.run(main.java:76)
    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:5081)
    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:791)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Object should first be initialized (BitmapDrawable).
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **

Using your advice

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
Dim p As Panel
Dim androidresources1 As AndroidResources
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("Layout1")
p.Initialize("me")
Activity.AddView(p, 0dip,0dip, 100%x, 100%y)
p.Color = Colors.White
Dim iv As ImageView
iv.Initialize("iv")
p.AddView(iv, 16dip, 12dip, 32dip, 32dip)
iv.Background = androidresources1.GetAndroidDrawable("ic_action_email")
End Sub

Works without a hitch but no image is loaded from the res folder. It returns null even though my xhdpi, hdpi etc folders all contain ic_action_email.png
 
Last edited:
Upvote 0

warwound

Expert
Licensed User
Longtime User
AndroidResources has a method GetAndroidDrawableNames which returns a List.
If you call this method and search the List values for "ic_action_email" do you find it?

Martin.
 
Upvote 0
Top