Android Question Using CreateScaledBitmap to change the wallpaper

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Can you tell me what additional coding I need to add to this to change the wallpaper to the file "img_0293.jpg" referenced in the coding?

Thanks.

B4X:
#Region  Project Attributes
    #ApplicationLabel: My App Label Goes Here
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals

End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)

    Dim b As Bitmap
    Activity.LoadLayout("main")
    b = LoadBitmap(File.DirAssets, "img_0293.jpg")
    b = CreateScaledBitmap(b, 100, 100, True)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub CreateScaledBitmap(Original As Bitmap, Width As Int, Height As Int, Filter As Boolean) As Bitmap
  Dim r As Reflector
  Dim b As Bitmap
  b = r.RunStaticMethod("android.graphics.Bitmap", "createScaledBitmap", _
      Array As Object(Original, Width, Height, Filter), _
      Array As String("android.graphics.Bitmap", "java.lang.int", "java.lang.int", "java.lang.boolean"))
  Return b
End Sub
 

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Erel,

Thanks for the fast reply.

I tried it using this code but the wallpaper shows just a grey screen.

Do I need to add anything extra?

Truly,
Emad

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    Activity.LoadLayout("main")
    SetWallPaper(LoadBitmap(File.DirAssets, "img_0293.jpg"))

   
End Sub

Sub SetWallPaper(Bmp As Bitmap)
   Dim r As Reflector
   r.Target = r.RunStaticMethod("android.app.WallpaperManager", "getInstance", _
      Array As Object(r.GetContext), Array As String("android.content.Context"))
   r.RunMethod4("setBitmap", Array As Object(Bmp), Array As String("android.graphics.Bitmap"))
End Sub
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Have you added the wallpaper permission?
Hi,

Yes. I did it like this:

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.basic4ppc.com/forum/showthread.php?p=78136
AddManifestText(
<uses-permission android:name="android.permission.SET_WALLPAPER"/>

<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I also tried this and the background image will show up. I hope to do the same for the wallpaper.

B4X:
    Activity.SetBackgroundImage(LoadBitmap(File.DirAssets, "img_0293.jpg"))
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Erel,

I moved to another computer and re-install B4A with the latest SDK and Java and your code works as is should. The wallpaper changed as expected.

I'm not sure why it didn't work on the other computer since it resulted in a gray screen on the Android device.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Erel,

I'm experiencing some problems with the wallpaper changer. It works but is very slow to respond.

I tried to upload the project but your server has a limit of 512kb.

when I click the top button to change the wallpaper, I click the exit button to exit button to close the app. Instead of immediately closing the app, it closes after 23 seconds have passed. I'm using a quad core Android 4.2 phone so I don't think that's what's causing it to take so long.

Can you look at the app and let me know what I did wrong?

Thanks.

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

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.

    Dim Button1 As Button
    Dim Button2 As Button
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")

    Activity.LoadLayout("main")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub SetWallPaper(Bmp As Bitmap)
  Dim r As Reflector
  r.Target = r.RunStaticMethod("android.app.WallpaperManager", "getInstance", _
      Array As Object(r.GetContext), Array As String("android.content.Context"))
  r.RunMethod4("setBitmap", Array As Object(Bmp), Array As String("android.graphics.Bitmap"))

End Sub

Sub Button1_Click
   
    SetWallPaper(LoadBitmap(File.DirAssets, "138.jpg"))
End Sub

Sub Button2_Click
   
    Activity.Finish
End Sub
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Erel,

I discovered the slow processing was caused because my photos were large so I reduced them down to 800 by 600 and it processed them in a decent amount of time so I can get each wallpaper to change every 15 seconds.
 
Upvote 0
Top