@@ -113,25 +113,27 @@ class MediaPickerUtils @Inject constructor(
113
113
}
114
114
}
115
115
116
- fun getMediaStoreFilePath (uri : Uri ): String? {
116
+ private fun getMediaStoreFilePath (uri : Uri ): String? {
117
117
var path: String? = null
118
118
if (VERSION .SDK_INT >= VERSION_CODES .Q ) {
119
119
try {
120
120
val cachedFile = createTempFile()
121
- val parcelFileDescriptor = context.contentResolver.openFile(uri, " r" , null )
122
- parcelFileDescriptor?.fileDescriptor?.let { fd ->
123
- val input = FileInputStream (fd)
124
- val byteArray = readBinaryStream(
125
- input,
126
- parcelFileDescriptor.statSize.toInt()
127
- )
128
- if (cachedFile != null ) {
129
- val fileSaved = writeFile(cachedFile, byteArray)
130
- if (fileSaved) {
131
- path = cachedFile.absolutePath
121
+ context.contentResolver.openFile(uri, " r" , null )
122
+ .use { parcelFileDescriptor ->
123
+ parcelFileDescriptor?.fileDescriptor?.let { fd ->
124
+ val input = FileInputStream (fd)
125
+ val byteArray = readBinaryStream(
126
+ input,
127
+ parcelFileDescriptor.statSize.toInt()
128
+ )
129
+ if (cachedFile != null ) {
130
+ val fileSaved = writeFile(cachedFile, byteArray)
131
+ if (fileSaved) {
132
+ path = cachedFile.absolutePath
133
+ }
134
+ }
132
135
}
133
136
}
134
- }
135
137
} catch (e: IOException ) {
136
138
log.e(e)
137
139
}
@@ -172,14 +174,12 @@ class MediaPickerUtils @Inject constructor(
172
174
}
173
175
174
176
private fun createTempFile (): File ? {
175
- var file: File ? = null
176
- try {
177
- val tempFileName = " temp-${System .currentTimeMillis()} .jpg"
178
- file = File (context.cacheDir, tempFileName)
177
+ return try {
178
+ File .createTempFile(" temp-${System .currentTimeMillis()} " , " .jpg" )
179
179
} catch (e: RuntimeException ) {
180
180
log.e(e)
181
+ null
181
182
}
182
- return file
183
183
}
184
184
185
185
private fun readBinaryStream (
0 commit comments