在 android Q 中,Payload.File.asJavaFile() 返回 null。尝试使用以下方法访问文件时出现异常。他们说在文档中使用 Payload.asFile().asUri() ,但是库中不再存在 asUri() 。我正在使用 17.0.0 版的 Google 附近连接 api
我的代码
override fun save(data: Payload) {
val input = FileInputStream(data.asFile()!!.asParcelFileDescriptor().fileDescriptor));
File copy = File(context.getFilesDir(),"copy".jpg);
val out = new FileOutputStream(copy)
//using kotlin extension
input.copyTo(out)// throws android.system.ErrnoException: read failed: EBADF (Bad file descriptor)
}
堆栈跟踪
Caused by: java.io.IOException: read failed: EBADF (Bad file descriptor)
at libcore.io.IoBridge.read(IoBridge.java:519)
at java.io.FileInputStream.read(FileInputStream.java:320)
at java.io.FileInputStream.read(FileInputStream.java:289)
at kotlin.io.ByteStreamsKt.copyTo(IOStreams.kt:106)
at kotlin.io.ByteStreamsKt.copyTo$default(IOStreams.kt:103)
at com.ume.airdrop.ui.components.file.ScopeStorage.copyFrom(ScopeStorage.kt:25)
at com.ume.airdrop.ui.components.file.BaseStorage$save$2.invoke(BaseStorage.kt:44)
at com.ume.airdrop.ui.components.file.BaseStorage$save$2.invoke(BaseStorage.kt:14)
at com.ume.airdrop.ui.components.file.BaseStorage$createFunction$1.invoke(BaseStorage.kt:90)
at com.ume.airdrop.ui.components.file.BaseStorage$createFunction$1.invoke(BaseStorage.kt:14)
at com.ume.airdrop.ui.components.file.BaseStorage.save(BaseStorage.kt:73)
at com.ume.airdrop.ui.service.ForegroundService.onComplete(ForegroundService.kt:96)
at com.ume.airdrop.ui.components.p2p.DataHandler.onPayloadTransferUpdate(DataHandler.kt:48)
at com.google.android.gms.internal.nearby.zzax.notifyListener(Unknown Source:14)
at com.google.android.gms.common.api.internal.ListenerHolder.notifyListenerInternal(Unknown Source:17)
at com.google.android.gms.common.api.internal.ListenerHolder$zaa.handleMessage(Unknown Source:5)
at android.os.Handler.dispatchMessage(Handler.java:106)
at com.google.android.gms.internal.base.zap.dispatchMessage(Unknown Source:8)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7660)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: android.system.ErrnoException: read failed: EBADF (Bad file descriptor)
at libcore.io.Linux.readBytes(Native Method)
at libcore.io.Linux.read(Linux.java:189)
at libcore.io.ForwardingOs.read(ForwardingOs.java:176)
at libcore.io.BlockGuardOs.read(BlockGuardOs.java:305)
at libcore.io.ForwardingOs.read(ForwardingOs.java:176)