Skip to content

Commit 9dbcfae

Browse files
committed
Close ParcelFileDescriptor
1 parent e0b549e commit 9dbcfae

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

app/src/main/kotlin/org/fossify/gallery/helpers/MotionPhotoDataSource.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import androidx.media3.common.util.UnstableApi
99
import androidx.media3.datasource.BaseDataSource
1010
import androidx.media3.datasource.DataSource
1111
import androidx.media3.datasource.DataSpec
12+
import android.os.ParcelFileDescriptor
1213
import java.io.File
1314
import java.io.RandomAccessFile
1415
import androidx.core.net.toUri
@@ -21,6 +22,7 @@ class MotionPhotoDataSource(
2122
) : BaseDataSource(false) {
2223

2324
private var randomAccessFile: RandomAccessFile? = null
25+
private var parcelFileDescriptor: ParcelFileDescriptor? = null
2426
private var inputStream: java.io.InputStream? = null
2527
private var bytesRemaining = 0L
2628
private var opened = false
@@ -40,6 +42,7 @@ class MotionPhotoDataSource(
4042
if (filePath.startsWith("content:/")) {
4143
val pfd = context.contentResolver.openFileDescriptor(filePath.toUri(), "r")
4244
?: throw java.io.FileNotFoundException("Cannot open $filePath")
45+
parcelFileDescriptor = pfd
4346
val fis = java.io.FileInputStream(pfd.fileDescriptor)
4447
fis.skip(videoOffset + position)
4548
inputStream = fis
@@ -83,6 +86,11 @@ class MotionPhotoDataSource(
8386
} finally {
8487
inputStream = null
8588
}
89+
try {
90+
parcelFileDescriptor?.close()
91+
} finally {
92+
parcelFileDescriptor = null
93+
}
8694
if (opened) {
8795
opened = false
8896
transferEnded()

0 commit comments

Comments
 (0)