我設法在我的應用程式中添加了服務(創建 Excel 檔案 - 創建 PDF 檔案)在這兩個服務中我都遇到了打開檔案包的問題..
我使用的包:
- open_document 1.0.5
- open_filex: ^4.1.1
- 打開檔案:^3.2.1
我為此使用了許多包,但我認為有一個問題我沒有通過在(AndroidManifest.xml)檔案 build.gradle(專案級別)中添加腳本來解決它。
我會告訴你我的代碼。
順便說一句,我使用的是GetX包,所以有Controller類:
import 'dart:io';
import 'dart:typed_data';
import 'package:get/get.dart';
import 'package:open_document/open_document.dart';
import 'package:path_provider/path_provider.dart';
import 'package:uuid/uuid.dart';
import '../model/bill_model.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
class BillController extends GetxController {
static BillController instance = BillController();
List<String> options = ['Cleaning Tax', 'Running Costs', 'Delay Tax'];
List<BillModel> taxOptions = [
BillModel(id: const Uuid().v1(), name: 'Cleaning Tax'),
BillModel(id: const Uuid().v1(), name: 'Running Costs'),
BillModel(id: const Uuid().v1(), name: 'Delay Tax'),
];
List<BillModel> selectedOptions = [];
Future<Uint8List> createPDF() async {
final pdf = pw.Document();
pdf.addPage(
pw.Page(
build: (context) {
return pw.Center(
child: pw.Text('Hello World'),
);
},
),
);
update();
return pdf.save();
}
Future savePdfFile(String fileName, Uint8List byteList) async {
final output = await getTemporaryDirectory();
var filePath = '${output.path}/$fileName.pdf';
final file = File(filePath);
await file.writeAsBytes(byteList);
await OpenDocument.openDocument(filePath: filePath);
update();
}
}
這是我在 UI 類中呼叫它的地方:
GetBuilder<BillController>(
builder: (controller) => Padding(
padding: EdgeInsets.only(top: Dimensions.height80),
child: AddSaveButton(
title: 'Create bill',
fontSize: Dimensions.font24,
onPress: () async {
final data = await controller.createPDF();
controller.savePdfFile('invoice_5', data);
},
),
),
),
我遇到了這個問題:
Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)
并從 StackOverFlow Unhandled Exception 中的此鏈接獲得解決方案:PlatformException(channel-error, Unable to setup on channel., null, null)
之后出現了新問題:app:processDebugResources并且在此鏈接中也獲得了 StackOverFlow 中可接受的答案:Flutter 專案中任務“:app:processDebugResources”的執行失敗
我的 AndroidManifest.xml 檔案:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.water_collection">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.open_document_example.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
<application
android:label="Water collection"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>
終于什么都沒有改善,但還是不知道為什么?!!
任何幫助將不勝感激。
uj5u.com熱心網友回復:
終于經過一周的搜索,我得到了解決方案。我所做的一切都是正確的,但問題出在“打開檔案”包中。我使用的所有包都有問題。
最后,我使用了一個名為的包open_file_plus,并且一切正常。
我從這個 URL 得到了一些資訊:Open file by default application flutter
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/521463.html
標籤:安卓擅长扑pdf创建文件
