Android Question File.Copy2 other Option ? not to Close the InputStream

alienhunter

Active Member
Licensed User
hi to All ,

I am reading a Blob from a Sql and output it as a Jpg File but File.Copy2 closes the stream
Is there another option ? not to close the Inputstream ?

thanks AH



B4X:
Private Sub ReadBytesFully(In As InputStream, Data() As Byte, Len As Int) As Byte()
  Dim count = 0, read As Int
  Do While count < Len AND read > -1
    read = In.ReadBytes(Data, count, Len - count)
    count = count + read
  Loop
   
 
  File.Copy2(In,File.OpenOutput(File.DirRootExternal,"3.jpg",False))
  Return Data
 
 
 
End Sub
 

alienhunter

Active Member
Licensed User

Sorry but i have to bug you again
this is in the Class Module DBRequestManager

i want to have 6 Pictures saved from the SQL to the sd Card when read the row
there will be always 6 in there


In the read module (Case T_BLOB) i put the read out but i cannot figure out how to keep the stream
open to read all other pictures (it reads just one ...) because the" File.Copy2(In,out) " closes the stream
Or is there a better way ...

B4X:
Private Sub ReadObject(In As InputStream) As Object
    Dim data(1) As Byte
    In.ReadBytes(data, 0, 1)
    Select data(0)

.....

        Case T_BLOB
            Dim len As Int = ReadInt(In)
            Dim data(len) As Byte
            Log( "PIC"& data.Length)
          
      
    Log( "PIC1")
    Dim out As OutputStream
    out=File.OpenOutput(File.DirRootExternal,"12.jpg",False)
    File.Copy2(In,out)
    out.close

      Return ReadBytesFully(In, data, data.Length)
          
          
        Case Else

thanks AH
 

Attachments

  • 12.jpg
    12.jpg
    33.2 KB · Views: 167
Upvote 0

alienhunter

Active Member
Licensed User
Thanks for your Help Erel
my brain...t


i got it now .... :cool:
works like a charm

B4X:
   If Job.JobName = "DBRequest" Then
  Dim result As DBResult = reqManager.HandleJob(Job)
  '  If result.Tag = " " Then 'query tag
  For Each records() As Object In  result.Rows
     
      Dim name As String=records(0)
      Dim name1 As String=records(1)
      Dim name2 As String=records(2)
      Dim name3 As String=records(3)
      Dim name4 As String=records(4)
      Dim name5 As String=records(5)

   Dim data() As Byte =records(14)' records(result.Columns.Get("jobnr"))
      Dim Ini As InputStream
      Ini.InitializeFromBytesArray(data,0,1)
      Log(data.Length)
      Dim out As OutputStream
      If File.Exists(File.DirRootExternal,"99.jpg") Then
      File.Delete(File.DirRootExternal,"99.jpg")
      End If
      out=File.OpenOutput(File.DirRootExternal,"99.jpg",True)
      out.WriteBytes(data, 0, data.Length)
      'File.Copy2(Ini,out)
      out.Flush
      out.Close

  Log(name&name1&name2&name3&name4&name5&data.Length)
  Next


thanks again
 

Attachments

  • works.jpg
    works.jpg
    25.2 KB · Views: 131
Upvote 0

alienhunter

Active Member
Licensed User
You should use File.Copy2 instead of WriteBytes. It is more efficient.

Thanks Erel but File.copy2 returns only 1 Byte ? (see jpg)

AH


B4X:
If Job.JobName = "DBRequest" Then
      Dim result As DBResult = reqManager.HandleJob(Job)
    '  If result.Tag = " " Then 'query tag
        For Each records() As Object In  result.Rows
      
          Dim name As String=records(0)
          Dim name1 As String=records(1)
          Dim name2 As String=records(2)
          Dim name3 As String=records(3)
          Dim name4 As String=records(4)
          Dim name5 As String=records(5)
        
          Dim data() As Byte =records(14)' records(result.Columns.Get("jobnr"))
          Dim Ini As InputStream
          Ini.InitializeFromBytesArray(data,0,1)
          Log(data.Length)
          Dim out As OutputStream
          If File.Exists(File.DirRootExternal,"99.jpg") Then
            File.Delete(File.DirRootExternal,"99.jpg")
          End If
          out=File.OpenOutput(File.DirRootExternal,"99.jpg",True)
          'out.WriteBytes(data, 0, data.Length)
          File.Copy2(Ini,out)
          out.Flush
          out.Close
        
          Dim data() As Byte =records(15)' records(result.Columns.Get("jobnr"))
          Dim Ini As InputStream
          Ini.InitializeFromBytesArray(data,0,1)
          Log(data.Length)
          Dim out As OutputStream
          If File.Exists(File.DirRootExternal,"100.jpg") Then
            File.Delete(File.DirRootExternal,"100.jpg")
          End If
          out=File.OpenOutput(File.DirRootExternal,"100.jpg",True)
          out.WriteBytes(data, 0, data.Length)
          'File.Copy2(Ini,out)
          out.Flush
          out.Close
 

Attachments

  • filecopy2.jpg
    filecopy2.jpg
    23.9 KB · Views: 135
Upvote 0
Top