Android Question why crash in InputStream1.ReadBytes(tem,0,1)

xinxirong

New Member
tutorial->FileTransfer,I change it to socket in block mode,no AsyncStreams

Public Sub SendFile(Dir As String, FileName As String)
Dim In As InputStream = File.OpenInput(Dir, FileName)
InputStream1=socket1.InputStream
OutputStream1=socket1.OutputStream

Dim tem(12) As Byte
countingStream.Initialize(In)
currentFile = FileName.SubString(FileName.LastIndexOf("/") + 1)
Dim buf1(6) As Byte
buf1(0)=2
buf1(1)=0x4C
buf1(2)=0x50
buf1(3)=0x30
buf1(4)=0x32
buf1(5)=0x0A
OutputStream1.WriteBytes(buf1,0,6)
InputStream1.ReadBytes(tem,0,1)'InputStream1.BytesAvailable=1

Dim buf2(17) As Byte
buf2(0)=2
buf2(1)=0x38
buf2(2)=0x34
buf2(3)=0x20
buf2(4)=0x63
buf2(5)=0x66
buf2(6)=0x41
buf2(7)=0x30
buf2(8)=0x30
buf2(9)=0x32
buf2(10)=0xD0
buf2(11)=0xC1
buf2(12)=0xCE
buf2(13)=0xFD
buf2(14)=0xC8
buf2(15)=0xD9
buf2(16)=0x0A
'astream.Write(buf2)
OutputStream1.WriteBytes(buf2,0,17)
InputStream1.ReadBytes(tem,0,1)

UpdateProgress
End Sub
 

xinxirong

New Member
ndroid 4+ doesn't allow applications to make network calls on the main thread. There is a good reason for this restriction as such calls cause the UI to freeze and after 5 second Android will show the "Application not responding" dialog.

Proper libraries take care of handling network calls with background threads. If you encounter a library that doesn't do it then you have two options:

1. Use the Threading library to start a background thread and make the calls from this thread.
2. Disable this check.

The code posted here disables this check (it only calls public APIs and is safe to use):
<code>
Sub DisableStrictMode
Dim jo As JavaObject
jo.InitializeStatic("android.os.Build.VERSION")
If jo.GetField("SDK_INT") > 9 Then
Dim policy As JavaObject
policy = policy.InitializeNewInstance("android.os.StrictMode.ThreadPolicy.Builder", Null)
policy = policy.RunMethodJO("permitAll", Null).RunMethodJO("build", Null)
Dim sm As JavaObject
sm.InitializeStatic("android.os.StrictMode").RunMethod("setThreadPolicy", Array(policy))
End If
End Sub
</Code>
 
Upvote 0
Top