|
|
|
@ -2,13 +2,14 @@ package io.legado.app.utils |
|
|
|
|
|
|
|
|
|
import android.content.Context |
|
|
|
|
import android.net.Uri |
|
|
|
|
import androidx.documentfile.provider.DocumentFile |
|
|
|
|
import java.io.File |
|
|
|
|
|
|
|
|
|
fun Uri.isContentScheme() = this.scheme == "content" |
|
|
|
|
|
|
|
|
|
@Throws(Exception::class) |
|
|
|
|
fun Uri.readBytes(context: Context): ByteArray? { |
|
|
|
|
if (this.toString().isContentScheme()) { |
|
|
|
|
if (this.isContentScheme()) { |
|
|
|
|
return DocumentUtils.readBytes(context, this) |
|
|
|
|
} else { |
|
|
|
|
val path = RealPathUtil.getPath(context, this) |
|
|
|
@ -32,7 +33,7 @@ fun Uri.writeBytes( |
|
|
|
|
context: Context, |
|
|
|
|
byteArray: ByteArray |
|
|
|
|
): Boolean { |
|
|
|
|
if (this.toString().isContentScheme()) { |
|
|
|
|
if (this.isContentScheme()) { |
|
|
|
|
return DocumentUtils.writeBytes(context, byteArray, this) |
|
|
|
|
} else { |
|
|
|
|
val path = RealPathUtil.getPath(context, this) |
|
|
|
@ -48,3 +49,22 @@ fun Uri.writeBytes( |
|
|
|
|
fun Uri.writeText(context: Context, text: String): Boolean { |
|
|
|
|
return writeBytes(context, text.toByteArray()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun Uri.writeBytes( |
|
|
|
|
context: Context, |
|
|
|
|
fileName: String, |
|
|
|
|
byteArray: ByteArray |
|
|
|
|
): Boolean { |
|
|
|
|
if (this.isContentScheme()) { |
|
|
|
|
DocumentFile.fromTreeUri(context, this)?.let { pDoc -> |
|
|
|
|
DocumentUtils.createFileIfNotExist(pDoc, fileName)?.let { |
|
|
|
|
return it.uri.writeBytes(context, byteArray) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
FileUtils.createFileWithReplace(path + File.separatorChar + fileName) |
|
|
|
|
.writeBytes(byteArray) |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
return false |
|
|
|
|
} |