fix(android): 保存/导出写入系统公共 Documents(可见),不再落到应用私有 data 目录
- 原生通道 seqstudio/docs:Android 10+ 走 MediaStore 写入 Documents/SequenceDiagram(免权限、商店合规);Android 9- 直接写公共目录(WRITE_EXTERNAL_STORAGE 限 maxSdk=28) - 安卓保存/打开/导出改用「Documents 对话框」:保存输入文件名(自动补全后缀),打开列出 Documents 中的 .seq.json 点选 - 控制器新增 markSaved(外部通道保存后标记干净);重新构建签名 APK
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<!-- 仅 Android 9 及以下直接写公共目录需要;Android 10+ 走 MediaStore 无需权限。 -->
|
||||
<uses-permission
|
||||
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
||||
android:maxSdkVersion="28" />
|
||||
<application
|
||||
android:label="时序图设计器"
|
||||
android:name="${applicationName}"
|
||||
|
||||
@@ -1,5 +1,176 @@
|
||||
package cc.sincos.sequence_diagram
|
||||
|
||||
import android.content.ContentValues
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Environment
|
||||
import android.provider.MediaStore
|
||||
import io.flutter.embedding.android.FlutterActivity
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
import java.io.File
|
||||
|
||||
class MainActivity : FlutterActivity()
|
||||
/**
|
||||
* 安卓公共文档读写通道(`seqstudio/docs`):
|
||||
* 保存 / 列出 / 读取 `Documents/SequenceDiagram` 下的文件。
|
||||
*
|
||||
* - API 29+(Android 10+):通过 MediaStore 写入公共 Documents,
|
||||
* 无需存储权限,符合应用商店的分区存储(scoped storage)要求;
|
||||
* - API ≤ 28:直接写入公共目录(配合 manifest 中 maxSdkVersion=28 的
|
||||
* WRITE_EXTERNAL_STORAGE 权限)。
|
||||
*/
|
||||
class MainActivity : FlutterActivity() {
|
||||
private val channelName = "seqstudio/docs"
|
||||
private val folderName = "SequenceDiagram"
|
||||
|
||||
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
|
||||
super.configureFlutterEngine(flutterEngine)
|
||||
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, channelName)
|
||||
.setMethodCallHandler { call, result ->
|
||||
when (call.method) {
|
||||
"saveFile" -> {
|
||||
val name = call.argument<String>("name")
|
||||
val data = call.argument<ByteArray>("data")
|
||||
val mime = call.argument<String>("mime")
|
||||
if (name == null || data == null) {
|
||||
result.error("ARGS", "name/data required", null)
|
||||
} else {
|
||||
try {
|
||||
result.success(saveFile(name, data, mime))
|
||||
} catch (e: Exception) {
|
||||
result.error("WRITE", e.message, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
"listFiles" -> {
|
||||
try {
|
||||
result.success(listFiles())
|
||||
} catch (e: Exception) {
|
||||
result.error("LIST", e.message, null)
|
||||
}
|
||||
}
|
||||
"readFile" -> {
|
||||
val name = call.argument<String>("name")
|
||||
if (name == null) {
|
||||
result.error("ARGS", "name required", null)
|
||||
} else {
|
||||
try {
|
||||
result.success(readFile(name))
|
||||
} catch (e: Exception) {
|
||||
result.error("READ", e.message, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
else -> result.notImplemented()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** MediaStore 相对路径(Q+);API ≤ 28 返回绝对路径。 */
|
||||
private fun documentsFolder(): String {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
return Environment.DIRECTORY_DOCUMENTS + "/" + folderName
|
||||
}
|
||||
@Suppress("DEPRECATION")
|
||||
val dir = File(
|
||||
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS),
|
||||
folderName,
|
||||
)
|
||||
if (!dir.exists()) dir.mkdirs()
|
||||
return dir.absolutePath
|
||||
}
|
||||
|
||||
/** 保存文件到公共 Documents/SequenceDiagram,返回用于展示的位置。 */
|
||||
private fun saveFile(name: String, data: ByteArray, mime: String?): String {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
val resolver = contentResolver
|
||||
val collection = MediaStore.Files.getContentUri("external_primary")
|
||||
val values = ContentValues().apply {
|
||||
put(MediaStore.Files.FileColumns.DISPLAY_NAME, name)
|
||||
put(MediaStore.Files.FileColumns.MIME_TYPE, mime ?: "application/octet-stream")
|
||||
put(MediaStore.Files.FileColumns.RELATIVE_PATH, documentsFolder())
|
||||
put(MediaStore.Files.FileColumns.IS_PENDING, 1)
|
||||
}
|
||||
val uri: Uri = resolver.insert(collection, values)
|
||||
?: throw IllegalStateException("MediaStore insert failed")
|
||||
resolver.openOutputStream(uri)?.use { it.write(data) }
|
||||
?: throw IllegalStateException("openOutputStream failed")
|
||||
values.clear()
|
||||
values.put(MediaStore.Files.FileColumns.IS_PENDING, 0)
|
||||
resolver.update(uri, values, null, null)
|
||||
return "Documents/$folderName/$name"
|
||||
}
|
||||
@Suppress("DEPRECATION")
|
||||
val dir = File(
|
||||
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS),
|
||||
folderName,
|
||||
)
|
||||
if (!dir.exists()) dir.mkdirs()
|
||||
File(dir, name).writeBytes(data)
|
||||
return "Documents/$folderName/$name"
|
||||
}
|
||||
|
||||
/** 列出 Documents/SequenceDiagram 下的文件([{name, size}],新在前)。 */
|
||||
private fun listFiles(): List<Map<String, Any>> {
|
||||
val out = mutableListOf<Map<String, Any>>()
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
val resolver = contentResolver
|
||||
val collection = MediaStore.Files.getContentUri("external_primary")
|
||||
val projection = arrayOf(
|
||||
MediaStore.Files.FileColumns._ID,
|
||||
MediaStore.Files.FileColumns.DISPLAY_NAME,
|
||||
MediaStore.Files.FileColumns.SIZE,
|
||||
)
|
||||
val selection = "${MediaStore.Files.FileColumns.RELATIVE_PATH}=?"
|
||||
val args = arrayOf(documentsFolder() + "/")
|
||||
resolver.query(collection, projection, selection, args, null)?.use { c ->
|
||||
val iName = c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DISPLAY_NAME)
|
||||
val iSize = c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.SIZE)
|
||||
while (c.moveToNext()) {
|
||||
out.add(mapOf("name" to c.getString(iName), "size" to c.getLong(iSize)))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
val dir = File(
|
||||
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS),
|
||||
folderName,
|
||||
)
|
||||
if (dir.exists()) {
|
||||
dir.listFiles()?.sortedByDescending { it.lastModified() }?.forEach {
|
||||
out.add(mapOf("name" to it.name, "size" to it.length()))
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/** 读取 Documents/SequenceDiagram 下的文件内容;不存在返回 null。 */
|
||||
private fun readFile(name: String): ByteArray? {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
val resolver = contentResolver
|
||||
val collection = MediaStore.Files.getContentUri("external_primary")
|
||||
val projection = arrayOf(MediaStore.Files.FileColumns._ID)
|
||||
val selection =
|
||||
"${MediaStore.Files.FileColumns.RELATIVE_PATH}=? AND " +
|
||||
"${MediaStore.Files.FileColumns.DISPLAY_NAME}=?"
|
||||
val args = arrayOf(documentsFolder() + "/", name)
|
||||
resolver.query(collection, projection, selection, args, null)?.use { c ->
|
||||
if (c.moveToFirst()) {
|
||||
val uri = Uri.withAppendedPath(collection, c.getLong(0).toString())
|
||||
return resolver.openInputStream(uri)?.use { it.readBytes() }
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
@Suppress("DEPRECATION")
|
||||
val f = File(
|
||||
File(
|
||||
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS),
|
||||
folderName,
|
||||
),
|
||||
name,
|
||||
)
|
||||
return if (f.exists()) f.readBytes() else null
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user