安卓查看导出记录及分享代码新增(有问题,需黄总协助)

This commit is contained in:
Daisy 2024-06-24 09:22:31 +08:00
parent 8cfbb14602
commit 8800f4bebf
6 changed files with 58 additions and 32 deletions

View File

@ -60,7 +60,18 @@
android:name="android.app.Application"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:usesCleartextTraffic="true">
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<meta-data
android:name="flutterEmbedding"
android:value="2" />

View File

@ -9,6 +9,8 @@ import io.flutter.plugin.common.MethodChannel
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.GeneratedPluginRegistrant
import android.bluetooth.BluetoothAdapter;
import androidx.core.content.FileProvider
import java.io.File
class MainActivity : FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
@ -16,9 +18,15 @@ class MainActivity : FlutterActivity() {
GeneratedPluginRegistrant.registerWith(flutterEngine!!)
MethodChannel(flutterEngine?.dartExecutor!!.binaryMessenger, "starLockFlutterSend").setMethodCallHandler { call, result ->
if (call.method == "loadNativeShare") {
var map = call.arguments as Map<String, String>
shareText(map["shareText"] , "分享")
} else if (call.method == "sendGetBlueStatus") {
val map = call.arguments as Map<String, String>
val shareText = map["shareText"]
val urlToShare = map["urlToShare"]
if (urlToShare == "fileShare") {
shareFile(shareText)
} else {
shareText(shareText, "分享")
}
} else if (call.method == "sendGetBlueStatus") {
// 蓝牙是否开启
// println("收到原生的信息了 methodmethodmethod: ${call.method}")
val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
@ -56,13 +64,41 @@ class MainActivity : FlutterActivity() {
startActivity(Intent.createChooser(shareIntent, null))
}
fun shareFile(filePath: String?) {
if (filePath == null) {
return
}
val file = File(filePath)
val uri: Uri = FileProvider.getUriForFile(
this,
"${applicationContext.packageName}.fileprovider",
file
)
val shareIntent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_STREAM, uri)
type = "application/octet-stream"
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
startActivity(Intent.createChooser(shareIntent, null))
}
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
GeneratedPluginRegistrant.registerWith(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "starLockFlutterSend").setMethodCallHandler { call, result ->
println("methodmethodmethod: ${call.method}")
// 在这里处理从 Flutter 发送过来的方法调用
if (call.method == "loadNativeShare") {
println("methodmethodmethod: ${call.method}")
val map = call.arguments as Map<String, String>
val shareText = map["shareText"]
val urlToShare = map["urlToShare"]
if (urlToShare == "fileShare") {
shareFile(shareText)
} else {
shareText(shareText, "分享")
}
} else {
result.notImplemented()
}

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="files" path="." />
</paths>

View File

@ -1,17 +1,13 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';
import 'package:star_lock/app_settings/app_colors.dart';
import 'package:star_lock/main/lockDetail/doorLockLog/exportSuccess/exportSuccess_logic.dart';
import 'package:star_lock/main/lockDetail/doorLockLog/exportSuccess/exportSuccess_state.dart';
import 'package:star_lock/tools/NativeInteractionTool.dart';
import 'package:star_lock/tools/submitBtn.dart';
import 'package:star_lock/tools/titleAppBar.dart';
import 'package:url_launcher/url_launcher.dart';
class ExportSuccessPage extends StatefulWidget {
const ExportSuccessPage({Key? key}) : super(key: key);
@ -94,23 +90,4 @@ class _ExportSuccessPageState extends State<ExportSuccessPage> with RouteAware {
],
);
}
Future<void> previewFile() async {
//
final Directory appDocDir = await getApplicationDocumentsDirectory();
final String appDocPath = appDocDir.path;
final String filePath = '$appDocPath/record.xlsx';
//
final File file = File(filePath);
if (await file.exists()) {
// 使url_launcher打开文件
final bool launched = await launchUrl(Uri.parse(filePath));
if (!launched) {
throw 'Could not launch $filePath';
}
} else {
print('File does not exist');
}
}
}