我开始使用Nearby,并通过流发送文件。我在发送方看到文件已发送,在接收方,我看到onPayloadReceived
事件和2 onPayloadTransferUpdate
个事件,第二个状态为1.一旦我收到该事件状态1,我运行以下代码:
Payload payload = payloads.remove(id);
try {
Payload.File payloadFile = payload.asFile();
Reader reader;
File file = payloadFile.asJavaFile();
if (file==null)
reader = new FileReader(payloadFile.asParcelFileDescriptor().getFileDescriptor());
else
reader = new FileReader(file);
StringBuilder builder = new StringBuilder();
char[] buff = new char[1024];
do
{
int count = reader.read(buff);
if (count<=0)
break;
builder.append(buff, 0, count);
}while(true);
receivedData.setText(builder);
}
catch (Exception exn){Log.d(TAG, "Exception thrown while receiving",exn);}
结果是file
为空,read
命令抛出IOException
消息read failed: EBADF (Bad file number)
。我该如何解决这个问题?
答案 0 :(得分:0)
您能否显示与您“通过流发送文件”对应的代码?
我问,因为STREAM和FILE是2种不同的Payload类型,所以如果你作为STREAM发送(无论STREAM的内容是否来自文件),你将收到STREAM。