flutter path_provider could not read downloaded files - flutter

I am using path_provider on flutter and would like to build Download file(mp4&jpg, from aws S3) and play it. I succeeded download and confirmed file is downloaded.(on ios)
But, when access the files using path_provider, raised error like below.
Unable to load asset: /Users/username/Library/Developer/CoreSimulator/Devices/B78BC67E-B127-4DD0-8A42-965EE645157B/data/Containers
the saving method is below
Future<File> _getFile(String filename) async {
final dir = await getApplicationDocumentsDirectory();
return File("${dir.path}/$filename");
}
final file = await _getFile("filename");
file.writeAsBytes(Bytes);
and the load method is below
final directory = await getApplicationDocumentsDirectory();
.
.
.
Image(image: AssetImage('${directory.path}/filename')) // /Users/username/Library/Developer/CoreSimulator/Devices/B78BC67E-B127-4DD0-8A42-965EE645157B/data/Containers/Data/Application/46EF23F5-840C-47D4-8314-B97CDEE2198C/Documents
.
.
.

Actually I need to use FileImage instead of Asset.
Thanks for GrahamD

Related

Download image from blob url

I need to download an image from a blob url.
I try using those solution, but none of them worked for me:
Flutter WebView blob pdf download
https://github.com/guoguoguilai/flutter-webview-blob-download
Display a bloc URL image in flutter)
final response = await get(
Uri.parse(blob),
);
final documentDirectory = await getApplicationDocumentsDirectory();
final File file = File(join(documentDirectory.path, 'image.png'))
..writeAsBytesSync(response.bodyBytes);
Does anyone have a possible solution ?

Is there any option to hide/ remove .hive files?

When I used hive as local db in flutter, .hive files are generated in user's app-directory. Is there any way to hide or remove these files from the app-directory?
You can remove file which contains the box and closes the box by using this method.
deleteFromDisk
Use path_provider to store hive file in getApplicationSupportDirectory
Future<void> init() async {
final path = await getApplicationSupportDirectory();
await Hive.initFlutter();
Hive.registerAdapter(UserAdapter());
await Hive.openBox<User>(HiveConst.userDataBox, path: path.path);
}

Flutter passing image string

i'm trying to send an image on instagram using flutter with the social_share package.
My issue is that i'm new with flutter and i don't find a proper way to send a path to the function SocialShare.shareInstagramStory(imageFile.path, "#ffffff", "#000000", "https://deep-link-url");
My images are located in the root of the flutter project in assets/images/my_images.
When i try to put the path SocialShare.shareInstagramStory("assets/images/my_image.jpeg");
i guess the phone doesn't know this path since it's looking on his own directory
hope i'm clear enough and thanks for your time!
Edit : here's my code:
onPressed: () async {
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
File file = File(tempDir.path);
final byteData = await rootBundle.load('assets/images/img_1.jpeg');
await file.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
await SocialShare.shareInstagramStory(file.path);
Navigator.of(context).pop();
},
i got this error :
E/flutter (12761): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: FileSystemException: Cannot open file, path = '/data/user/0/com.example.app/cache' (OS Error: Is a directory, errno = 21)
So basically, what you need to do is convert your asset image into a file, then create a temporary path and store this file there. Then you can reference that path
We need to create a temporary path with this plugin https://pub.dev/packages/path_provider
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
Then create a file
File file = File(tempDir.path+'/img_1.jpeg');//You might have to play arounf with the /. Remove it or put it in the end to see what works
Now we need to write the image to this
final byteData = await rootBundle.load('assets/images/img_1.jpeg');
await file.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
Now you can use this temporary path to do what you want

How do you download an image from the internet onto a file in Flutter?

I'm making a Flutter project where an image is displayed in listview and the user can share, favourite or download the image. I'm stuck on the downloading part. Is it possible to save an image file onto the phone's storage for offline use?
What I want to happen is I can create and write an image file
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
Future<File> get _localFile async {
final path = await _localPath;
return File('$path/$title.txt');
}
// Write an image by obtaining an image through a URL
and then be able to access it through
Image.file('$path/$title.txt')

How to play local mp3 file with audioplayer plugin in Flutter

How to play local mp3 file with audioplayer 0.2.0 in Flutter.
pubspec.yaml
flutter:
assets:
- sounds/music.mp3
main.dart
Future<ByteData> loadAsset() async {
return await rootBundle.load('sounds/music.mp3');
}
// FIXME: This code is not working.
Future playLocal() async {
final result = await audioPlayer.play(loadAsset());
if (result == 1) setState(() => playerState = PlayerState.playing);
}
Can I get local assets path from rootBundle?
Can I pay audio from ByteData on audioPlayer?
The audioplayer plugin currently only supports network paths and files. You can move an asset to a temporary folder and play it with this code:
import 'package:path_provider/path_provider.dart';
...
final file = new File('${(await getTemporaryDirectory()).path}/music.mp3');
await file.writeAsBytes((await loadAsset()).buffer.asUint8List());
final result = await audioPlayer.play(file.path, isLocal: true);
I have just created a repo in github that uses audioplayers plugin to stream local mp3 files. This player can seek, pause, stop and play local audio
https://github.com/samupra/local_flutter_audio_player
A way that worked for me was to create the assets file and load the mp3 file in to the directory.
Then load the file using the Future
AudioPlayer audio = await AudioCache.play("filetobeloaded.mp3");