我使用 ParcelFileDescriptor 将数据帧发送到连接的设备,但是当我检查错误日志时,我看到了这个错误。
03-03 02:06:12.896 4047-4133/shaveyourneck.syn.app.com.shaveyourneck W/NearbyConnections: Exception copying stream for Payload -7679084608943108956
java.io.IOException: File descriptor closed
at libcore.io.Posix.readBytes(Native Method)
at libcore.io.Posix.read(Posix.java:147)
at libcore.io.BlockGuardOs.read(BlockGuardOs.java:230)
at libcore.io.IoBridge.read(IoBridge.java:512)
at java.io.FileInputStream.read(FileInputStream.java:177)
at com.google.android.gms.common.util.zzn.zza(Unknown Source)
at com.google.android.gms.internal.zzcon.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
03-03 02:06:12.901 1118-4134/? E/NearbyConnections: PayloadManager short-circuiting Payload (id:-7679084608943108956 type:STREAM) after sending 0 bytes because we failed to detach the next chunk.
java.io.IOException: Error status received for Payload (id:-7679084608943108956 type:STREAM)
at yll.c(:com.google.android.gms@11975230:15)
at yme.a(:com.google.android.gms@11975230:29)
at yme.call(:com.google.android.gms@11975230)
at yjd.a(:com.google.android.gms@11975230:4)
at ymd.run(:com.google.android.gms@11975230:46)
at mwh.run(:com.google.android.gms@11975230:26)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at nbq.run(:com.google.android.gms@11975230)
at java.lang.Thread.run(Thread.java:818)
这是我的代码片段:
private void initFramesToBeSendCallback() {
// this is where i initiate the parcelfiledescriptor to the outputstream in the parcelfiledescriptor[1]
outputStream = new ParcelFileDescriptor.AutoCloseOutputStream(writePfd);
// Camera surface callback which returns camera frames data that i use to send write it in outputstream and send to connected device
surfaceHolder.setFramesToBeRetrieveListener(new CustomSurfaceHolder.OnCaptureFramesToBeSendCallback() {
@Override
public void getCurrentData(byte[] bytes) {
if (isRecording) {
Log.e(TAG, "initFramesToBeSendCallback RECORDING, will send data!");
send1stArrayOfParcelFileDescriptor(readPfd);
sendFramesOfImages(bytes);
} else {
Log.e(TAG, "initFramesToBeSendCallback NOT RECORDING");
}
}
});
}
private void send1stArrayOfParcelFileDescriptor(ParcelFileDescriptor fd) {
Nearby.Connections.sendPayload(client, endpointString, Payload.fromStream(fd))
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if (status.isSuccess() || status.getStatusCode() == 0) {
Log.e(TAG, "onResult: STREAM DATA SUCCESS send1stArrayOfParcelFileDescriptor");
} else {
Log.e(TAG, "onResult: FAILED TO SEND STREAM DATA ERROR send1stArrayOfParcelFileDescriptor CODE: " + status.getStatusCode());
}
}
});
}
private void sendFramesOfImages(byte[] bytes) {
byteRef = bytes;
new Thread(new Runnable() {
@Override
public void run() {
try {
outputStream.write(byteRef, 0, byteRef.length);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "sendFramesOfImages: EXCEPTION SENDING PARCELFILE DESCRIPTOR[1] " + e.getMessage());
}
}
}).start();
}
我不知道是什么引起的错误以及这背后的其他原因。我在谷歌搜索中找不到任何相关的错误。我希望来自 Google Devs 的人可以帮助我。谢谢