Issue
I want to download the file in my app and I used Dio
, path_provider
, permission_handler
so I should write something in the android manifest for android and info for ios.
and I did it. but it did not work and I've get this error when I clicked on the download button:
I/flutter (11919): Directory: '/storage/emulated/0/Android/data/com.example.podkadeh/files'
I/flutter (11919): FileSystemException: Cannot open file, path = '/storage/emulated/0/podkadeh/Image/image.jpg' (OS Error: Permission denied, errno = 13)
I/flutter (11919): Problem Downloading File
this OS Error: Permission denied, errno = 13
I wrote this lines in manifest:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION"/>
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage"/>
<uses-permission android:name="android.permission"
and wrote this in Application:
android:requestLegacyExternalStorage="true"
but still, it's not working well and I can't download it
here is my code for download file:
class DownloadImage extends StatefulWidget {
const DownloadImage (
{Key? key,})
: super(key: key);
@override
_DownloadImage State createState() => _DownloadImage State();
}
class _DownloadImageState extends State<DownloadImage > {
Future<bool> saveImageUrl(String url, String fileName) async {
Directory? directory;
try {
if (Platform.isAndroid) {
if (await _requestPermission(Permission.storage)) {
directory = await getExternalStorageDirectory();
String newPath = "";
print(directory);
List<String> paths = directory!.path.split("/");
for (int x = 1; x < paths.length; x++) {
String folder = paths[x];
if (folder != "Android") {
newPath += "/" + folder;
} else {
break;
}
}
newPath = newPath + "/podkadeh" + "/Image";
directory = Directory(newPath);
} else {
return false;
}
} else {
if (await _requestPermission(Permission.photos)) {
directory = await getTemporaryDirectory();
} else {
return false;
}
}
File saveFile = File(directory.path + "/$fileName");
if (!await directory.exists()) {
await directory.create(recursive: true);
}
if (await directory.exists()) {
await dio.download(url, saveFile.path,
onReceiveProgress: (value1, value2) {
setState(() {
progress = value1 / value2;
});
});
if (Platform.isIOS) {
await ImageGallerySaver.saveFile(saveFile.path,
isReturnPathOfIOS: true);
}
return true;
}
return false;
} catch (e) {
print(e);
return false;
}
}
Future<bool> _requestPermission(Permission permission) async {
if (await permission.isGranted) {
return true;
} else {
var result = await permission.request();
if (result == PermissionStatus.granted) {
return true;
}
}
return false;
}
downloadFile() async {
setState(() {
loading = true;
progress = 0;
});
bool downloadedImage = await saveImageUrl(
"https://test.podkadeh.ir/image-cache/Ep-61c9c0152ab37f246dd35a65-500.jpg",
"image.jpg");
if (downloadedImage) {
print("File Downloaded");
} else {
print("Problem Downloading File");
}
setState(() {
loading = false;
});
}
@override
Widget build(BuildContext context) {
return (progress != 0 && progress != 100)
? CircularProgressIndicator(
backgroundColor: loading ? Colors.amber : Colors.black,
valueColor:
AlwaysStoppedAnimation(loading ? Colors.blue : Colors.pink),
strokeWidth: progress,
value: progress,
)
: IconButton(
icon: SvgPicture.asset(MyIcons.frame, color: MyColors.black,height: 20,width: 20,),
onPressed: downloadFile,
padding: const EdgeInsets.all(10),
);
}
}
by the way I use compileSdkVersion 31
and I use this in gradle.properties
:
android.useAndroidX=true
android.enableJetifier=true
Solution
I solved this problem with the following solution:
instead of just asking for Permission.storage
i added two more permission requests Permission.accessMediaLocation , Permission.manageExternalStorage
in order to support all versions of Android.
try {
if (Platform.isAndroid) {
if (await _requestPermission(Permission.storage) &&
await _requestPermission(Permission.accessMediaLocation) &&
await _requestPermission(Permission.manageExternalStorage)) {
directory = await getExternalStorageDirectory();
String newPath = "";
print(directory);
.......
the result was:
Performing hot restart...
Syncing files to device Android SDK built for x86...
Restarted application in 2,177ms.
I/flutter ( 3712): Directory: '/storage/emulated/0/Android/data/com.example.untitled/files'
I/flutter ( 3712): File Downloaded
Answered By - Sadra
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.