我想做一个应用程序与Flutter ,可以安装其他APK.当我try 使用flutter_app_installer
或app_installer
个包安装APK时,手机上会弹出一个消息:"解析该包时出错."
通常,此错误消息表示.apk文件已损坏.然而,使用Android上的文件资源管理器,我可以很好地安装APK.
此外,可能存在许可问题.我的应用程序请求存储权限和安装APK的权限,我批准了这些权限.
如上所述,我已经try 了安装APK的不同Ffltter包.两者都抛出相同的错误消息.
当我故意提供到APK的错误路径时,我没有收到错误消息.我这样做是为了验证到APK的路径是否正确.
我试着在两台安卓设备上运行这个程序.
Flutter 调试控制台中没有显示错误消息,仅在电话上显示.
Here the error message (on a german phone):
我试着用谷歌搜索和询问闲聊,但找不到任何有用的建议.我在这一点上被困了一段时间.
这是我的Flutter 翼代码:
import 'dart:io';
import 'package:app_installer/app_installer.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:path/path.dart' as path;
import 'package:flutter_app_installer/flutter_app_installer.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Request runtime permission
await Permission.storage.request();
await Permission.requestInstallPackages.request();
runApp(const MainApp());
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: TextButton(
onPressed: () async {
await AppInstaller.installApk(
"storage/emulated/0/Download/test.apk",
);
},
child: const Text('Install App'),
),
),
),
);
}
}
这是我的Android Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.apk_install_test">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<application
android:label="apk_install_test"
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>
<!-- Provider -->
<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>
<!-- 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>