【问题标题】:Flutter: Accessing File: /storage/emulated/0/Android/data?颤振:访问文件:/storage/emulated/0/Android/data?
【发布时间】:2020-05-19 05:31:01
【问题描述】:

我正在制作一个在单击按钮时截取整个屏幕的应用程序。显然图像转到: /storage/emulated/0/Android/data/com.example.program-name/files

我已经使用 android 模拟器以及我的物理设备对此进行了测试。路径名称略有变化,但我仍然无法访问此图像。

我正在使用以下代码截取屏幕截图并保存:

takeScreenShot() async{
    RenderRepaintBoundary boundary = globalKey.currentContext.findRenderObject();
    ui.Image image = await boundary.toImage();
    final directory = (await getExternalStorageDirectory()).path;
    print(directory);
    ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    Uint8List pngBytes = byteData.buffer.asUint8List();
    File imgFile = new File('$directory/screenshot.png');
    print (imgFile.toString());
    imgFile.writeAsBytes(pngBytes);
  }

有没有办法:

  1. 访问此文件并将其移动到计算机/手机上的其他位置,或者,
  2. 将以下目录更改为可存放您计算机上任何文件的目录?

final directory = (await getExternalStorageDirectory()).path;

我们的想法是将用户数据保存在表单上,​​并保存刚刚填写的页面图像以供将来参考。例如,提交表单,将屏幕截图保存到 PC 桌面图库。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    这方面存在问题。 #48208#35783

    在Android上你可以自己写路径。

    Future<void> _screenshot() async {
        RenderRepaintBoundary boundary = globalKey.currentContext.findRenderObject();
        ui.Image image = await boundary.toImage();
        ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
        Uint8List pngBytes = byteData.buffer.asUint8List();
        Directory d = Directory('/storage/emulated/0');
        if (d.existsSync()) {
          Directory(d.path + '/MyApp').createSync();
          File imgFile = new File(d.path + '/MyApp/screenshot.png');
          print('saving to ${imgFile.path}');
          imgFile.createSync();
          imgFile.writeAsBytes(pngBytes);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-30
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 1970-01-01
      • 2022-10-02
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      相关资源
      最近更新 更多