我正在尝试制作上传功能,以便用户可以将文件上传到 firebase 帐户 以前运行得很好,但是昨天就不显示文件了
有代码
uploadImage() async {
final storage = FirebaseStorage.instance;
final picker = ImagePicker();
PickedFile? image;
//Check Permissions
await Permission.photos.request();
var permissionStatus = await Permission.photos.status;
if (permissionStatus.isGranted) {
//Select Image
image = await picker.getImage(source: ImageSource.gallery);
var file = File(image!.path);
final fileName = basename(file.path);
final destination = 'user/$emaila/identitas/$fileName';
if (image != null) {
//Upload to Firebase
var snapshot = await storage
.ref()
.child(destination)
.putFile(file)
.whenComplete(() => null);
var downloadUrl = await snapshot.ref.getDownloadURL();
setState(() {
imageUrlidentitas = downloadUrl;
});
} else {
print('No Path Received');
}
} else {
print('Grant Permissions and try again');
}
}
这是 Android 清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rekammedis">
<uses-permission android:name="android.permission.INTERNET"/>
<!-- Permissions options for the `storage` group -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<!-- Permissions options for the `camera` group -->
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:label="rekammedis"
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>
编译器说
D/permissions_handler(9318):在清单中找不到以下权限:[]9 D/permissions_handler(9318):在清单中找不到以下权限:[]9 我/flutter(9318):授予权限并重试
如何解决这个问题?有人知道吗?
我尝试在 stackoverflow 中查找,但没有一个能解释答案
这是最近的一个错误,是由 10.2.0 中新版本的权限处理程序引入的。这里讨论了https://github.com/Baseflow/flutter-permission-handler/issues/944,我也会复制这个问题的答案。这个解决方案也对我有用。
当设备为 Android 12.0 或更低版本时,我通过检查 Permissions.storage.status 修复了此问题,否则我使用 Permissions.photos.status。我使用device_info_plus插件来检查Android版本:
我将以下内容添加到我的 AndroidManifest.kt 中
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32"/>
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
并且颤抖:
if (Platform.isAndroid) {
final androidInfo = await DeviceInfoPlugin().androidInfo;
if (androidInfo.version.sdkInt <= 32) {
/// use [Permissions.storage.status]
} else {
/// use [Permissions.photos.status]
}
}
所有作品均属于 github 上的 HinrikHelga
在
build.gradle
文件中检查您的 targetSdkVersion。
如果您使用
targetSdkVersion = 30
,则需要以不同的方式写入存储权限。我想你可以尝试这篇post中讨论的解决方案
我认为你应该调用这个函数:
Permission.storage.request()
而不是
Permission.photos.request()