|
|
@ -10,32 +10,41 @@ import io.legado.app.exception.NoStackTraceException |
|
|
|
import io.legado.app.lib.permission.Permissions |
|
|
|
import io.legado.app.lib.permission.Permissions |
|
|
|
import io.legado.app.lib.permission.PermissionsCompat |
|
|
|
import io.legado.app.lib.permission.PermissionsCompat |
|
|
|
import java.io.File |
|
|
|
import java.io.File |
|
|
|
|
|
|
|
import java.io.FileInputStream |
|
|
|
|
|
|
|
import java.io.InputStream |
|
|
|
|
|
|
|
|
|
|
|
fun Uri.isContentScheme() = this.scheme == "content" |
|
|
|
fun Uri.isContentScheme() = this.scheme == "content" |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 读取URI |
|
|
|
* 读取URI |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun AppCompatActivity.readUri(uri: Uri?, success: (name: String, bytes: ByteArray) -> Unit) { |
|
|
|
fun AppCompatActivity.readUri( |
|
|
|
|
|
|
|
uri: Uri?, |
|
|
|
|
|
|
|
success: (fileDoc: FileDoc, inputStream: InputStream) -> Unit |
|
|
|
|
|
|
|
) { |
|
|
|
uri ?: return |
|
|
|
uri ?: return |
|
|
|
try { |
|
|
|
try { |
|
|
|
if (uri.isContentScheme()) { |
|
|
|
if (uri.isContentScheme()) { |
|
|
|
val doc = DocumentFile.fromSingleUri(this, uri) |
|
|
|
val doc = DocumentFile.fromSingleUri(this, uri) |
|
|
|
doc ?: throw NoStackTraceException("未获取到文件") |
|
|
|
doc ?: throw NoStackTraceException("未获取到文件") |
|
|
|
val name = doc.name ?: throw NoStackTraceException("未获取到文件名") |
|
|
|
val fileDoc = FileDoc.fromDocumentFile(doc) |
|
|
|
val fileBytes = DocumentUtils.readBytes(this, doc.uri) |
|
|
|
contentResolver.openInputStream(uri)!!.use { inputStream -> |
|
|
|
success.invoke(name, fileBytes) |
|
|
|
success.invoke(fileDoc, inputStream) |
|
|
|
|
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
PermissionsCompat.Builder(this) |
|
|
|
PermissionsCompat.Builder(this) |
|
|
|
.addPermissions( |
|
|
|
.addPermissions( |
|
|
|
Permissions.READ_EXTERNAL_STORAGE, |
|
|
|
Permissions.READ_EXTERNAL_STORAGE, |
|
|
|
Permissions.WRITE_EXTERNAL_STORAGE |
|
|
|
Permissions.WRITE_EXTERNAL_STORAGE |
|
|
|
) |
|
|
|
) |
|
|
|
.rationale(R.string.bg_image_per) |
|
|
|
.rationale(R.string.get_storage_per) |
|
|
|
.onGranted { |
|
|
|
.onGranted { |
|
|
|
RealPathUtil.getPath(this, uri)?.let { path -> |
|
|
|
RealPathUtil.getPath(this, uri)?.let { path -> |
|
|
|
val imgFile = File(path) |
|
|
|
val file = File(path) |
|
|
|
success.invoke(imgFile.name, imgFile.readBytes()) |
|
|
|
val fileDoc = FileDoc.fromFile(file) |
|
|
|
|
|
|
|
FileInputStream(file).use { inputStream -> |
|
|
|
|
|
|
|
success.invoke(fileDoc, inputStream) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
.request() |
|
|
|
.request() |
|
|
@ -49,26 +58,31 @@ fun AppCompatActivity.readUri(uri: Uri?, success: (name: String, bytes: ByteArra |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 读取URI |
|
|
|
* 读取URI |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun Fragment.readUri(uri: Uri?, success: (name: String, bytes: ByteArray) -> Unit) { |
|
|
|
fun Fragment.readUri(uri: Uri?, success: (fileDoc: FileDoc, inputStream: InputStream) -> Unit) { |
|
|
|
uri ?: return |
|
|
|
uri ?: return |
|
|
|
try { |
|
|
|
try { |
|
|
|
if (uri.isContentScheme()) { |
|
|
|
if (uri.isContentScheme()) { |
|
|
|
val doc = DocumentFile.fromSingleUri(requireContext(), uri) |
|
|
|
val doc = DocumentFile.fromSingleUri(requireContext(), uri) |
|
|
|
doc ?: throw NoStackTraceException("未获取到文件") |
|
|
|
doc ?: throw NoStackTraceException("未获取到文件") |
|
|
|
val name = doc.name ?: throw NoStackTraceException("未获取到文件名") |
|
|
|
val fileDoc = FileDoc.fromDocumentFile(doc) |
|
|
|
val fileBytes = DocumentUtils.readBytes(requireContext(), doc.uri) |
|
|
|
requireContext().contentResolver.openInputStream(uri)!!.use { inputStream -> |
|
|
|
success.invoke(name, fileBytes) |
|
|
|
success.invoke(fileDoc, inputStream) |
|
|
|
|
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
PermissionsCompat.Builder(this) |
|
|
|
PermissionsCompat.Builder(this) |
|
|
|
.addPermissions( |
|
|
|
.addPermissions( |
|
|
|
Permissions.READ_EXTERNAL_STORAGE, |
|
|
|
Permissions.READ_EXTERNAL_STORAGE, |
|
|
|
Permissions.WRITE_EXTERNAL_STORAGE |
|
|
|
Permissions.WRITE_EXTERNAL_STORAGE |
|
|
|
) |
|
|
|
) |
|
|
|
.rationale(R.string.bg_image_per) |
|
|
|
.rationale(R.string.get_storage_per) |
|
|
|
.onGranted { |
|
|
|
.onGranted { |
|
|
|
RealPathUtil.getPath(requireContext(), uri)?.let { path -> |
|
|
|
RealPathUtil.getPath(requireContext(), uri)?.let { path -> |
|
|
|
val imgFile = File(path) |
|
|
|
val file = File(path) |
|
|
|
success.invoke(imgFile.name, imgFile.readBytes()) |
|
|
|
val fileDoc = FileDoc.fromFile(file) |
|
|
|
|
|
|
|
FileInputStream(file).use { inputStream -> |
|
|
|
|
|
|
|
success.invoke(fileDoc, inputStream) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
.request() |
|
|
|
.request() |
|
|
|