libpdfviewer/app/src/main/java/app/grapheneos/pdfviewer/KtUtils.kt

31 lines
878 B
Kotlin
Raw Normal View History

2022-04-22 07:34:05 +02:00
package app.grapheneos.pdfviewer
import android.content.Context
import android.net.Uri
import java.io.FileNotFoundException
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
@Throws(
FileNotFoundException::class,
IOException::class,
IllegalArgumentException::class,
OutOfMemoryError::class
)
2022-04-22 07:34:05 +02:00
fun saveAs(context: Context, existingUri: Uri, saveAs: Uri) {
context.asInputStream(existingUri)?.use { inputStream ->
context.asOutputStream(saveAs)?.use { outputStream ->
outputStream.write(inputStream.readBytes())
}
}
}
@Throws(FileNotFoundException::class)
private fun Context.asInputStream(uri: Uri): InputStream? = contentResolver.openInputStream(uri)
@Throws(FileNotFoundException::class)
private fun Context.asOutputStream(uri: Uri): OutputStream? = contentResolver.openOutputStream(uri)