Android Question Identify plant - no such file error

I am trying to identify plants using plantnet API (https://my-api.plantnet.org/?tags=my-api#/my-api/postV2IdentifyProject). but i keep getting file not found Please help!
** Activity (main) Resume **
The media directory is /data/user/0/b4a.example/files/shared and the file name is mediachooser-temp-1 and the mime is image/jpeg .
The filename is mediachooser-temp-1.jpeg and directory is /data/user/0/b4a.example/files/shared
Error occurred on line: 164 (HttpJob)
java.io.FileNotFoundException: /data/user/0/b4a.example/files/shared/mediachooser-temp-1.jpeg: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:574)
at java.io.FileInputStream.<init>(FileInputStream.java:160)
at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:215)
at b4a.example.httpjob._postmultipart(httpjob.java:182)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
at b4a.example.b4xmainpage._uploadimage(b4xmainpage.java:161)
at b4a.example.b4xmainpage$ResumableSub_Button1_Click.resume(b4xmainpage.java:124)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)


I take a photo using mediachooser as below:

take photo:
Private Sub Button1_Click
    Wait For (chooser.CaptureImage) Complete (Result As MediaChooserResult)
    Log($"The media directory is ${Result.MediaDir} and the file name is ${Result.MediaFile} and the mime is ${Result.Mime} ."$)
    ShowMedia(Result)
    uploadImage(Result.MediaFile&"."&"jpeg",Result.MediaDir)
End Sub
Private Sub ShowMedia (Result As MediaChooserResult)
    If Result.Success Then
        smm.SetMediaFromFile(Panel1, Result.MediaDir, Result.MediaFile, Result.Mime, Null)
    Else
        Panel1.RemoveAllViews
    End If
End Sub
I upload the image to API as follows :

upload image:
Sub uploadImage (filename As String, dir As String)
    Log($"The filename is ${filename} and directory is ${dir}"$)
    Dim j As HttpJob
    j.Initialize("", Me)
   
    ' Create multipart file data
    Dim mp As MultipartFileData
    mp.Initialize
    mp.Dir = dir
    mp.FileName = filename
    mp.KeyName = "images" ' key for the image
    mp.ContentType = "image/jpeg" ' content type
   
    ' Prepare additional form fields (organs)
    Dim map As Map
    map.Initialize
    map.Put("organs", "leaf")
   
    ' Set the API URL with your API key
    Dim api_endpoint As String = $"https://my-api.plantnet.org/v2/identify/all?include-related-images=false&no-reject=false&nb-results=10&lang=en&type=kt&api-key=${API_KEY}"$
   
    ' Perform the POST request
    j.PostMultipart(api_endpoint, map, Array(mp))
   
    ' Set the required headers
    j.GetRequest.SetHeader("accept", "application/json")
    j.GetRequest.SetHeader("Content-Type", "multipart/form-data")
   
    ' Wait for the response
    Wait For (j) JobDone(j As HttpJob)
   
    If j.Success Then
        ' Parse the JSON response
        Dim parser As JSONParser
        parser.Initialize(j.GetString)
        Dim jRoot As Map = parser.NextObject
        Dim preferedReferential As String = jRoot.Get("preferedReferential")
        Dim bestMatch As String = jRoot.Get("bestMatch")
       
        Log("Prefered Referential: " & preferedReferential)
        Log("Best Match: " & bestMatch)
    Else
        Log("Error: " & j.ErrorMessage)
    End If
   
    j.Release
End Sub
 

drgottjr

Expert
Licensed User
Longtime User
did someone tell you to add ".jpeg" to the resource's name?
 
Upvote 1
Solution
Top