Jes*_*You 3 storage image file dart flutter
所以我想访问设备上特定目录中的文件,但我不知道如何具体,我只想让应用程序进入设备内部存储并从某些特定位置获取一些文件
在此文件列表中,我想访问 XENDER 目录中的其他文档
在XENDER目录中我现在想要访问IMAGES文档中的文件
现在在这里只是希望能够在列表视图中列出我的 flutter 应用程序中的图像,当单击其中任何一个时,我可以将图像保存在文件中
像这样
File file = await asset.file;
Run Code Online (Sandbox Code Playgroud)
这样我就可以在应用程序的其他地方使用它
您应该查看pub.dev上的路径提供程序插件。您想要做的是读取 Xender/image 目录并将它们映射到列表或其他内容。请参阅下面我的实现。我试图以交错视图显示图像(假设图像为 jpg 格式):首先,将路径提供程序插件添加到您的 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
),),
),
),
);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7118 次 |
| 最近记录: |