0

In android Q, Payload.File.asJavaFile() returns null. Getting exception when trying to access the file using the approach below. They said to use Payload.asFile().asUri() in the documentation, however asUri() does not exist in the library anymore. I'm using version 17.0.0 of Google nearby connections api

My Code


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)

}

StackTrace

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)
Prodigy
  • 105
  • 1
  • 6
  • This is very bad code if you come here for help as we cannot copy/paste it to try out. Nobody knows what generateName() would give. Or packet.mime. or getUri(). And so on. Please rewrite. – blackapps May 13 '21 at 17:35
  • I have updated the code – Prodigy May 13 '21 at 18:25
  • Does the permission error come from the FileInputStream or from the FileOutputStream? You should have posted the stack trace from the logcat long ago. Also show full path of the file you wanna copy. Start your code with it. Make it copy and paste. – blackapps May 13 '21 at 19:19
  • It comes from the FileInputStream. The input stream comes from Google Nearby connections api. FileInputStream(Payload.asFile().asParcelFileDescriptor().fileDescriptor) – Prodigy May 13 '21 at 19:37
  • Trying the to read the input stream created from the FileDescriptor throws an Exception – Prodigy May 13 '21 at 19:39
  • 1
    Well post full code in your post. I'm asking the whole time to show what you do. And if it is the input stream that throws an error then why is in the subject of your post `when writing to media uri`? And where is the logcat stacktrace? Why are you refusing to post it here? – blackapps May 13 '21 at 19:48
  • @blackapps please check my updated question – Prodigy May 13 '21 at 20:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232360/discussion-between-prodigy-and-blackapps). – Prodigy May 13 '21 at 20:05
  • Can someone help me out??? – Prodigy May 13 '21 at 22:49

0 Answers0