【问题标题】:Unzip a file without the aggressive MANAGE_EXTERNAL_STORAGE解压缩没有激进的 MANAGE_EXTERNAL_STORAGE 的文件
【发布时间】:2021-11-18 04:14:07
【问题描述】:

我正在 Flutter 中构建一个面向 Android 30+ 的小应用程序,它需要下载一组文件并将其解压缩到一个目录中。

当面向 SDK 29 及更低版本时,一切都按预期工作,但是当移动到 30+ 时,除非我开始使用 android.permission.MANAGE_EXTERNAL_STORAGE,否则我会被拒绝权限,这似乎有点过分解压缩文件。

我已尝试将文件下载到多个不同的目录(电话 Tmp、应用程序支持和应用程序文档)以查看是否有帮助,但没有成功。

我可以使用其他类型的权限或解决此问题的其他方法吗?

【问题讨论】:

  • Documents 目录似乎是执行此操作的好地方,对于 SDK 30 及更高版本,您不需要 MANAGE_EXTERNAL_STORAGE
  • 我已经尝试过了,但仍然被拒绝权限,你能提供一个链接到它应该工作的详细信息吗?
  • 您能否添加您尝试过的内容,因为我不太熟悉 Flutter,但我可以建议一些更改

标签: android flutter android-permissions


【解决方案1】:

使用getApplicationDocumentsDirectory();getCacheDir() 您将不需要权限。

示例:

import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

void main() {
  runApp(
    MaterialApp(
      title: 'Reading and Writing Files',
      home: FlutterDemo(storage: CounterStorage()),
    ),
  );
}

class CounterStorage {
  Future<String> get _localPath async {
    final directory = await getApplicationDocumentsDirectory();

    return directory.path;
  }

  Future<File> get _localFile async {
    final path = await _localPath;
    return File('$path/counter.txt');
  }

  Future<int> readCounter() async {
    try {
      final file = await _localFile;

      // Read the file
      final contents = await file.readAsString();

      return int.parse(contents);
    } catch (e) {
      // If encountering an error, return 0
      return 0;
    }
  }

  Future<File> writeCounter(int counter) async {
    final file = await _localFile;

    // Write the file
    return file.writeAsString('$counter');
  }
}

class FlutterDemo extends StatefulWidget {
  const FlutterDemo({Key? key, required this.storage}) : super(key: key);

  final CounterStorage storage;

  @override
  _FlutterDemoState createState() => _FlutterDemoState();
}

class _FlutterDemoState extends State<FlutterDemo> {
  int _counter = 0;

  @override
  void initState() {
    super.initState();
    widget.storage.readCounter().then((int value) {
      setState(() {
        _counter = value;
      });
    });
  }

  Future<File> _incrementCounter() {
    setState(() {
      _counter++;
    });

    // Write the variable as a string to the file.
    return widget.storage.writeCounter(_counter);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Reading and Writing Files'),
      ),
      body: Center(
        child: Text(
          'Button tapped $_counter time${_counter == 1 ? '' : 's'}.',
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

【讨论】:

  • 这是不正确的,您误读了我的问题。我可以创建、读取和删除文件,但不能使用这些权限修改现有文件。
  • @ChrisTurner 提到您的问题,您不需要修改任何文件。解压zip文件不需要修改!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-10
  • 1970-01-01
  • 2018-11-06
  • 1970-01-01
  • 2010-09-05
  • 1970-01-01
  • 2013-03-09
相关资源
最近更新 更多