flutter Android版本10创建管理外部存储的问题,而代码在Android 11中运行良好

7hiiyaii  于 2022-12-24  发布在  Flutter
关注(0)|答案(2)|浏览(213)

我尝试下载文件。所以在android 11我需要管理外部存储权限。这些权限我已经给了,他们与android 11工作正常。下载正在发生,文件正在存储。但当我与android 10工作,然后有一个错误没有权限在清单中找到22,我在互联网上搜索,发现这是错误

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
    tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


void getPermission() async {
var status = await Permission.storage.request();
var status3 = await Permission.manageExternalStorage.request();
if (status.isGranted && status3.isGranted) {
  // print('granted');
  //var status2 = await Permission.accessMediaLocation.request();
  //if (status2.isGranted) {
  setState(() {
    _allowWriteFile = true;
  });
} else {
  // print("denied");
}

}

z4iuyo4d

z4iuyo4d1#

我遇到了同样的问题,我这样做解决。对于android 10,你必须在mainActivity中给予权限,

class MainActivity: FlutterActivity() {

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {

        GeneratedPluginRegistrant.registerWith(flutterEngine);

        if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.Q){
            setupPermissions()
        }
    }

    private fun setupPermissions() {

        if (ContextCompat.checkSelfPermission(this@MainActivity,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE) !==
            PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this@MainActivity,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                ActivityCompat.requestPermissions(this@MainActivity,
                    arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE), 1)
            } else {
                ActivityCompat.requestPermissions(this@MainActivity,
                    arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE), 1)
            }
        }
    }
u59ebvdq

u59ebvdq2#

尝试在android manifest文件中添加以下内容,inside application标记.

<application
   android:requestLegacyExternalStorage="true" ...  </application>

相关问题