【Flutter】AndroidでDownloadフォルダにファイルを保存する方法

androidのあたらしいバージョンはパーミッションとかではできないらしいので
https://developer.android.com/training/data-storage?hl=ja

パーミッションを許可するダイアログを表示させる

permission_handler: ^10.0.0
if (await Permission.storage.status.isDenied) {
  if (await Permission.storage.request().isDenied) {
    return false; // 拒絶された((+_+))
  }
}

ダウンロードフォルダのパスを取得

external_path: ^1.0.3
path = await ExternalPath.getExternalStoragePublicDirectory(
          ExternalPath.DIRECTORY_DOWNLOADS);

保存

import 'dart:io';

  String fullPath = '$path/$file_name';
    final file = File(fullPath);
    await file.create();
    return await file.writeAsBytes(res.bodyBytes).then((value) {
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
        content: Text('$fullPathに保存されました'),
      ));
      return true;
    });

Flutter開発で知らないと損すること Flutter開発で知らないと損すること

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です