Android Question Java error with Imagedownloader

Discussion in 'Android Questions' started by thestar19, Jul 8, 2015.

  1. thestar19

    thestar19 Member Licensed User

    Hey

    So I'm trying to write an app that downloads 365 or 52 images and then saves them.

    Up until now I have been using a webview and CaptureBitmap(). After sleeping on it I realized that it probably would be better to use Imagedownloader.

    Please ignore all surrounding code, it all works with the rest of the app. It was working with CaptureBitmap() so please focus on the Imagedownloader service since it seems to be the error creator.
    Even though I have not counted, I have encountered a similar error with the link I am using to download images. Last time the symbol "{" was the problem. I do not know if this is the case again but I do recognize the "Illegal character at index 99" error.
    Also, my code for saving the image is horrible but since I had huge problems getting it to work, I have been reluctant to change it. If any of you B4X gurus have a better idea, feel free to suggest it.

    When I try to download the images using Imagedownloader I get the following error message:


    Code:
    ** Activity (main) Create, isFirst = true **
    ** 
    Activity (main) Resume **
    ** 
    Service (imagedownloader) Create **
    ** 
    Service (imagedownloader) Start **
    Error occurred on line: 
    37 (ImageDownloader)
    java.lang.IllegalArgumentException: Illegal character 
    in query at index 99: http://www.novasoftware.se/ImgGen/schedulegenerator.aspx?format=png&schoolid=52550/sv-se&type=1&id={877B6E23-FA02-44CC-9967-12A26CCD6FA3}&period=&week=1&mode=0&printer=0&Colors=32&head=0&clock=0&foot=0&day=1&width=1080&height=1557&maxwidth=1080&maxheight=1557
        at java.net.URI.create(URI.java:730)
        at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
        at anywheresoftware.b4a.http.HttpClientWrapper$HttpUriRequestWrapper.InitializeGet(HttpClientWrapper.java:342)
        at anywheresoftware.b4a.samples.httputils2.httpjob._download(httpjob.java:67)
        at com.emmalex.blackeberg.imagedownloader._download(imagedownloader.java:157)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
        at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
        at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
        at anywheresoftware.b4a.BA$3.run(BA.java:334)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

    The important part of my own code is:
    Code:
    Sub capture_website(bmp As Bitmap, how_many_bitmaps As Int )
    'Restart app/Go to main_meny
    '/old code
    If main_map_saved.Get("use_weeks") = "true" Then
    ' Use weeks
    Dim out2 As OutputStream
    out2.InitializeToBytesArray(
    0)
    bmp.WriteToStream(out2, 
    100"PNG")
    out2.Close
    Dim data() As Byte = out2.ToBytesArray
    Dim out As OutputStream = File.OpenOutput(File.DirInternal, "image_to_save" & current_download_number & ".png"False)
    out.WriteBytes(data, 
    0, data.Length)
    out.Close
    If how_many_bitmaps = 52 Then
    'old code
    main_map_saved.Put("first_time","nope")
    Msgbox("All nödvändig data har laddats ner, Appen kommer nu stängas av!","Information")
    Activity.Finish()
    End If
    End If
    If main_map_saved.Get("use_days") = "true" Then
    ' use days
    Dim out2 As OutputStream
    out2.InitializeToBytesArray(
    0)
    bmp.WriteToStream(out2, 
    100"PNG")
    out2.Close
    Dim data() As Byte = out2.ToBytesArray
    Dim out As OutputStream = File.OpenOutput(File.DirInternal, "image_to_save" & current_download_number  & ".png"False)
    out.WriteBytes(data, 
    0, data.Length)
    out.Close
    If how_many_bitmaps = 365 Then
    'old code
    main_map_saved.Put("first_time","nope")
    Msgbox("All nödvändig data har laddats ner, Appen kommer nu stängas av!","Information")
    Activity.Finish()
    End If
    End If
    End Sub
    I have slightly modiefied the Imagedownloader service and now it looks like this:
    Code:
    #Region  Service Attributes
        
    #StartAtBoot: False
    #End Region

    Sub Process_Globals
        
    Private cache As Map
        
    Private tasks As Map
        
    Private ongoingTasks As Map
        
    Dim times = 0 As Int
    End Sub

    Sub Service_Create
        tasks.Initialize
        
    cache.Initialize
        ongoingTasks.Initialize
    End Sub

    Sub Service_Start (StartingIntent As Intent)

    End Sub

    Sub Service_Destroy

    End Sub

    Sub Download (ImageViewsMap As Map)
        
    For i = 0 To ImageViewsMap.Size - 1
            tasks.Put(ImageViewsMap.GetKeyAt(i), ImageViewsMap.GetValueAt(i))
            
    Dim link As String = ImageViewsMap.GetValueAt(i)
            
    If cache.ContainsKey(link) Then
                
    Dim iv As ImageView = ImageViewsMap.GetKeyAt(i)
                iv.SetBackgroundImage(
    cache.Get(link))
            
    Else If ongoingTasks.ContainsKey(link) = False Then
                ongoingTasks.Put(link, 
    "")
                
    Dim j As HttpJob
                j.Initialize(link, Me)
                j.Download(link)
            
    End If
        
    Next
    End Sub

    Sub JobDone(Job As HttpJob)
        times = times+
    1
        ongoingTasks.Remove(Job.JobName)
        
    If Job.Success Then
            
    Dim bmp As Bitmap = Job.GetBitmap
            
    cache.Put(Job.JobName, bmp)
            
    If tasks.IsInitialized Then
                
    For i = 0 To tasks.Size - 1
                    
    Dim link As String = tasks.GetValueAt(i)
                    
    If link = Job.JobName Then
                        
    'Dim iv As ImageView = tasks.GetKeyAt(i)
                        'iv.SetBackgroundImage(bmp)
                        CallSubDelayed3(Main, "capture_website", bmp, times )
                    
    End If
                
    Next
            
    End If
        
    Else
            
    Log("Error downloading image: " & Job.JobName & CRLF & Job.ErrorMessage)
        
    End If
        Job.Release
    End Sub
    Sub ActivityIsPaused
        tasks.Clear
    End Sub
    Thank you
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    You need to "url encode" the parameters. Usually the simplest solution is to use Job.Download2 instead of Job.Download and pass the parameters.

    However as you are using ImageDownloader you will need to url encode the parameters yourself. I recommend you to go over HttpJob code and see how to do it.
     
Loading...