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