How can I access files in a particular directory in my flutter app

4,304

You should check out the path provider plugin on pub.dev. What you want to do is read the Xender/image directory and say map them to a list or something. See my implementation below. I am trying to show the images in a staggered view(assuming the images are in jpg format): First, add the path provider plugin to your pubspec.yaml

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';

final Directory _photoDir = Directory('/storage/emulated/0/Xender/image');

class Photos extends StatefulWidget {
  @override
  PhotosState createState() {
    return new PhotosState();
  }
}

class PhotosState extends State {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if(!Directory("${_photoDir.path}").existsSync()) {
      return Scaffold(
        appBar: AppBar(
          title: Text("Xender Images"),
        ),
        body: Container(
          padding: EdgeInsets.only(bottom: 60.0),
          child: Center(
            child: Text("All Xender images should appear here", style: TextStyle(
              fontSize: 18.0
            ),),
          ),
        ),
      );
    }else {
      var imageList = _photoDir.listSync().map((item) => item.path).where((
          item) => item.endsWith(".jpg")).toList(growable: false);

      if (imageList.length > 0) {
        return Scaffold(
          appBar: AppBar(
            title: Text("Xender Images"),
          ),
          body: Container(
            padding: EdgeInsets.only(bottom: 60.0),
            child: StaggeredGridView.countBuilder(
              padding: const EdgeInsets.all(8.0),
              crossAxisCount: 4,
              itemCount: imageList.length,
              itemBuilder: (context, index) {
                String imgPath = imageList[index];
                return Material(
                  elevation: 8.0,
                  borderRadius: BorderRadius.all(Radius.circular(8)),
                  child: InkWell(
                    onTap: () {},
                    child: Hero(
                      tag: imgPath,
                      child: Image.file(
                        File(imgPath),
                        fit: BoxFit.cover,
                      ),
                    ),
                  ),
                );
              },
              staggeredTileBuilder: (i) =>
                  StaggeredTile.count(2, i.isEven ? 2 : 3),
              mainAxisSpacing: 8.0,
              crossAxisSpacing: 8.0,
            ),
          ),
         );
      } else {
        return Scaffold(
          appBar: AppBar(
            title: Text("Xender images"),
          ),
          body: Center(
            child: Container(
              padding: EdgeInsets.only(bottom: 60.0),
              child: Text("Sorry, No Images Where Found.", style: TextStyle(
                  fontSize: 18.0
              ),),
            ),
          ),
        );
      }
    }
  }
}
Share:
4,304
Jesus Loves You
Author by

Jesus Loves You

Updated on December 21, 2022

Comments

  • Jesus Loves You
    Jesus Loves You over 1 year

    so I want to access files in a particular directory on my device but i do not know how to In particular I just want the app to go into the devices internal storage and get some files from some particular locations

    So this is the devices list of files

    In this list of files i want to access the other documents inside the XENDER directory

    enter image description here

    And in the XENDER directory I now want to access the files in the IMAGES document

    enter image description here

    Now over here Just want to be able to list the images in my flutter app in a listview and when any of them is clicked i can get the image saved in a file

    like this

    File file = await asset.file;
    

    so i can use it in other places in the app

  • Jesus Loves You
    Jesus Loves You almost 4 years
    Ok thanks, am yet to try it, I will now but can I please ask what exactly the storage/emulated/0 means? is every devices storage arranged like that
  • Mastersam
    Mastersam almost 4 years
    storage/emulated/0 is the device root directory or what you would call the phone local storage.
  • Eggy
    Eggy over 2 years
    storage/emulated/0 it is possible in ios?