Flutter passing image string - flutter

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

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 it possible change asset docx file and reopen it in flutter

I used path-provider and open-file for open docx file from asset which worked fine without conflict in my project, then I needed to change some text in docx file. After changing it gave me an error like
Unable to open the document, decoding error
here is my open file method code
onPressed: () async {
Future<File> openFileFromAsset() async {
Directory tempDir = await getTemporaryDirectory();
File tempFile = File('${tempDir.path}/$fileName.docx');
ByteData bd = await rootBundle.load(assetName);
await tempFile.writeAsBytes(bd.buffer.asUint8List(), flush: true);
return tempFile;
}
openFileFromAsset().then((file){OpenFile.open(file.path);});
},
I want to fix the error

flutter path_provider could not read downloaded files

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

How can I share voice file(mpeg) which in app files to other app(like whatsapp) in flutter

I tried flutter share 2.0.1 pluggin for share voice file but it did not work(No such file). Could you solve this problem or how can I share voice file to other app? Here is my code and error screenshot.
E/flutter (31567): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: PlatformException(assets/voices/pırt.mpeg (No such file or directory), null, null, null)
E/flutter (31567): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:597:7)
E/flutter (31567): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:158:18)
IconButton(
icon: Icon(Icons.send),
color: Colors.black,
iconSize: 35,
onPressed: () {
try{
Share.shareFiles(["assets/voices/pırt.mpeg"],text: "Share");
}
catch(ex){
print(ex);
}
},
),
As the error states, assets/voices/pırt.mpeg is not a valid path to a file on the OS. That's an asset, packaged into your application. If you want to share assets, you need to make them a file on the device first.
You'll need to add the path_provider dependency:
dependencies:
path_provider: ^2.0.1
Then, make a new File and write the data from the asset to the File:
//Get directory and make a file object
final Directory dir = await getTemporaryDirectory();
final File file = File(dir.path + '/mpeg_data');
//Get data from assets
ByteData data = await rootBundle.load('assets/voices/pırt.mpeg');
//Write actual data
await file.writeAsBytes(data.buffer.asUint8List());
This should all go before you share the file. Then when you share the file, use the path of the file object you created:
//Get directory and make a file object
final Directory dir = await getTemporaryDirectory();
final File file = File(dir.path);
//Get data from assets
ByteData data = await rootBundle.load('assets/voices/pırt.mpeg');
//Write actual data
await file.writeAsBytes(data.buffer.asUint8List());
try{
Share.shareFiles([file.path],text: "Share");
}
catch(ex){
print(ex);
}

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')