Android Question FTP Upload

davelt99

Member
Licensed User
Longtime User
Within the app the user takes multiple pictures and stores them as blobs to a SQLLITE db.

Then the images, along with a description of each image, are exported from the database and uploaded to a server via FTP - one at a time inside of a For/Next loop.

My problem is that the first image and description are uploaded correctly but successive images are duplicates of the first image while successive descriptions are correct.

I've attached a small text file with the code.

Thank you,
Any help would be greatly appreciated.
 

Attachments

  • ftpUpload.txt
    997 bytes · Views: 147

Mahares

Expert
Licensed User
Longtime User
Did you try to create a list of all the image files first before uploading them. Perhaps something like this. See my added lines with the comment 'new:
B4X:
Dim SQL1      As SQL
            Dim sFTP      As FTP_Auto
            Dim waitTimer As Timer

Dim MyList as list     'New
Dim MyFile as String  'New
MyList.Initialize          'New

      sFTP.Initialize(Activity,Me,"IPadresss","userID","password",21,True,False)
            sFTP.SetCompletedUploadDialog(True,"Uploads Completed...")
            SQL1.Initialize(File.DirInternal, "images.db", False)
      Dim jC As Cursor
      jC = SQL1.ExecQuery("SELECT * FROM images where uploaded='No'") 
      For i=0 To jC.RowCount-1
               jC.Position=i
          Buffer = jC.GetBlob("image")
          Dim out2 As OutputStream
          '' save real filename to the phone
          out2 = File.OpenOutput(File.DirRootExternal, jC.GetString("picName"),False)
          out2.WriteBytes(Buffer, 0, Buffer.Length)
   MyList.add(jC.GetString("picName"))    'New
          out2.Flush
                 out2.Close
                                                                  
             waitTimer.Initialize("waitTimer",4000)
                    waitTimer.Enabled=True
        Next
for i= 0 to myList.Size-1      'New
          MyFile=Mylist.get(i)           'New
         sFTP.UpLoadFile(File.DirRootExternal, "/visuals/",MyFile)    'New
next            'New
            sFTP.Close
            jC.Close
            SQL1.Close
 
Upvote 0

davelt99

Member
Licensed User
Longtime User
Did you try to create a list of all the image files first before uploading them. Perhaps something like this. See my added lines with the comment 'new:
B4X:
Dim SQL1      As SQL
            Dim sFTP      As FTP_Auto
            Dim waitTimer As Timer

Dim MyList as list     'New
Dim MyFile as String  'New
MyList.Initialize          'New

      sFTP.Initialize(Activity,Me,"IPadresss","userID","password",21,True,False)
            sFTP.SetCompletedUploadDialog(True,"Uploads Completed...")
            SQL1.Initialize(File.DirInternal, "images.db", False)
      Dim jC As Cursor
      jC = SQL1.ExecQuery("SELECT * FROM images where uploaded='No'")
      For i=0 To jC.RowCount-1
               jC.Position=i
          Buffer = jC.GetBlob("image")
          Dim out2 As OutputStream
          '' save real filename to the phone
          out2 = File.OpenOutput(File.DirRootExternal, jC.GetString("picName"),False)
          out2.WriteBytes(Buffer, 0, Buffer.Length)
   MyList.add(jC.GetString("picName"))    'New
          out2.Flush
                 out2.Close
                                                                 
             waitTimer.Initialize("waitTimer",4000)
                    waitTimer.Enabled=True
        Next
for i= 0 to myList.Size-1      'New
          MyFile=Mylist.get(i)           'New
         sFTP.UpLoadFile(File.DirRootExternal, "/visuals/",MyFile)    'New
next            'New
            sFTP.Close
            jC.Close
            SQL1.Close


Thanks Mahares. But that didn't seem to work either. Found the answer though. The problem was in the way that the images were being inserted into the DB. I was copying the newly taken picture to:

InputStream1 = File.OpenInput(File.DirRootExternal, "image1.jpg")
Dim OutputStream1 As OutputStream
OutputStream1.InitializeToBytesArray(1000)
File.Copy2( InputStream1, OutputStream1)
Dim Buffer() As Byte 'declares an empty array
Buffer = OutputStream1.ToBytesArray
SQL1.ExecNonQuery2("INSERT INTO images VALUES('" & sJobID & "','" & sPicName & "','" & Wid & "','" & Ht & "','" & _
su.EncodeUrl(sPicNotes,"UTF8") & "','No',?)", Array As Object(Buffer))


Using "image1.jpg" each time didn't work

Instead:
Dim out As OutputStream
out = File.OpenOutput(File.DirRootExternal, sPicName, False)
out.WriteBytes(Data, 0, Data.Length)
out.close


File.OpenOutput(File.DirRootExternal,sPicName,False)
InputStream1 = File.OpenInput(File.DirRootExternal, sPicName)
Dim OutputStream1 As OutputStream
OutputStream1.InitializeToBytesArray(1000)
File.Copy2( InputStream1, OutputStream1)
Dim Buffer() As Byte 'declares an empty array
Buffer = OutputStream1.ToBytesArray
SQL1.ExecNonQuery2("INSERT INTO images VALUES('" & sJobID & "','" & sPicName & "','" & Wid & "','" & Ht & "','" & _
su.EncodeUrl(sPicNotes,"UTF8") & "','No',?)", Array As Object(Buffer))

SQL1.Close
InputStream1.Close
OutputStream1.Close

Probably some redundancy here, but it works.

Thanks again for your efforts.
 
Upvote 0
Top