|
|
|
@ -25,7 +25,6 @@ fun AppCompatActivity.readUri(uri: Uri?, success: (name: String, bytes: ByteArra |
|
|
|
|
doc ?: throw NoStackTraceException("未获取到文件") |
|
|
|
|
val name = doc.name ?: throw NoStackTraceException("未获取到文件名") |
|
|
|
|
val fileBytes = DocumentUtils.readBytes(this, doc.uri) |
|
|
|
|
fileBytes ?: throw NoStackTraceException("读取文件出错") |
|
|
|
|
success.invoke(name, fileBytes) |
|
|
|
|
} else { |
|
|
|
|
PermissionsCompat.Builder(this) |
|
|
|
@ -59,7 +58,6 @@ fun Fragment.readUri(uri: Uri?, success: (name: String, bytes: ByteArray) -> Uni |
|
|
|
|
doc ?: throw NoStackTraceException("未获取到文件") |
|
|
|
|
val name = doc.name ?: throw NoStackTraceException("未获取到文件名") |
|
|
|
|
val fileBytes = DocumentUtils.readBytes(requireContext(), doc.uri) |
|
|
|
|
fileBytes ?: throw NoStackTraceException("读取文件出错") |
|
|
|
|
success.invoke(name, fileBytes) |
|
|
|
|
} else { |
|
|
|
|
PermissionsCompat.Builder(this) |
|
|
|
@ -83,24 +81,24 @@ fun Fragment.readUri(uri: Uri?, success: (name: String, bytes: ByteArray) -> Uni |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Throws(Exception::class) |
|
|
|
|
fun Uri.readBytes(context: Context): ByteArray? { |
|
|
|
|
if (this.isContentScheme()) { |
|
|
|
|
return DocumentUtils.readBytes(context, this) |
|
|
|
|
fun Uri.readBytes(context: Context): ByteArray { |
|
|
|
|
return if (this.isContentScheme()) { |
|
|
|
|
DocumentUtils.readBytes(context, this) |
|
|
|
|
} else { |
|
|
|
|
val path = RealPathUtil.getPath(context, this) |
|
|
|
|
if (path?.isNotEmpty() == true) { |
|
|
|
|
return File(path).readBytes() |
|
|
|
|
File(path).readBytes() |
|
|
|
|
} else { |
|
|
|
|
throw NoStackTraceException("获取文件真实地址失败\n${this.path}") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return null |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Throws(Exception::class) |
|
|
|
|
fun Uri.readText(context: Context): String? { |
|
|
|
|
readBytes(context)?.let { |
|
|
|
|
fun Uri.readText(context: Context): String { |
|
|
|
|
readBytes(context).let { |
|
|
|
|
return String(it) |
|
|
|
|
} |
|
|
|
|
return null |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Throws(Exception::class) |
|
|
|
|