Reputation: 291
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
In this list of files i want to access the other documents inside the XENDER directory
And in the XENDER directory I now want to access the files in the IMAGES document
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
Upvotes: 3
Views: 7795
Reputation: 1535
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
),),
),
),
);
}
}
}
}
Upvotes: 6