@@ -19,6 +19,9 @@ import android.os.OperationCanceledException
1919import android.os.ParcelFileDescriptor
2020import android.os.ProxyFileDescriptorCallback
2121import android.os.storage.StorageManager
22+ import android.system.ErrnoException
23+ import android.system.Os
24+ import android.system.OsConstants.SEEK_SET
2225import android.util.Log
2326import androidx.core.content.getSystemService
2427import androidx.core.os.BundleCompat
@@ -161,10 +164,19 @@ class GramophoneAlbumArtProvider : ContentProvider() {
161164 })
162165 ) {
163166 (result.source.metadata as ContentMetadata ).assetFileDescriptor.let {
164- cfd.complete(AssetFileDescriptor (it
165- .parcelFileDescriptor.dup(), it.startOffset, it.declaredLength))
167+ val newFd = it.parcelFileDescriptor.dup()
168+ try {
169+ // It's only safe to give the unmodified fd to reader if it is
170+ // seekable, because otherwise (pipe?) no matter when we dup, we
171+ // would eat some data when checking for JPEG header.
172+ Os .lseek(newFd.fileDescriptor, it.startOffset,
173+ SEEK_SET )
174+ cfd.complete(AssetFileDescriptor (newFd, it.startOffset,
175+ it.declaredLength))
176+ } catch (_: ErrnoException ) {}
166177 }
167- } else {
178+ }
179+ if (! cfd.isCompleted) {
168180 writeDataCommon(cfd, scope, options.context) {
169181 if (it != null ) {
170182 it.sink().buffer().writeAll(src); null
0 commit comments