I'm trying to download an Azure Blob, which is a WAV file. I used azblob package to download the file and it returns a Streamed Response.
This is what I've done so far.
// Download WAV file from Azure's Blob Storage Container
var file = await storage.getBlob(wavFileLink);
print(file);
This is what Debug Console shows for print(file).
How do I save the StreamedResponse to WAV File in Flutter? Thank you in advance.
You can collect the data as byte data with the following code:
var file = await storage.getBlob(wavFileLink);
Uint8List data = await file.stream.toBytes();
If you really want to save the data as a file, you'll need to use the path_provider package:
dependencies:
path_provider: ^2.0.1
import 'package:path_provider/path_provider.dart';
//Obtain the directory you want. This is application documents.
var dir = await getApplicationDocumentsDirectory();
var sysFile = File('${dir.path}/file.wav');
sysFile.writeAsBytes(data);
Related
I want to copy an audio file to music folder in the storage. I have the path where the audio is, like '/data/user/0/app/audioFile' and I am passing to the function as 'audioPath'. I need help to finish this function, what can I do?
void _downloadAudio(audioPath) async {
var file = File(audioPath);
//get the music folder
await file.copy(music folder path);
}
You can use the getExternalStorageDirectory method from the dart:io library to get the path to the external storage directory on the device. This is where you can store files that are meant to be shared between apps or accessible to the user, such as music files. You can then use this path to construct the full path to the desired music folder. Here's an updated version of the function that will copy the audio file to the music folder:
import 'dart:io';
void _downloadAudio(String audioPath) async {
var file = File(audioPath);
// Get the external storage directory
final externalStorageDirectory = await getExternalStorageDirectory();
// Construct the full path to the music folder
final musicFolderPath = '${externalStorageDirectory.path}/Music';
// Check if the music folder exists, and create it if it doesn't
final musicFolder = Directory(musicFolderPath);
if (!await musicFolder.exists()) {
await musicFolder.create();
}
// Copy the audio file to the music folder
await file.copy('$musicFolderPath/${file.basename}');
}
Note that accessing the external storage directory requires the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions, so you'll need to request these permissions from the user if your app doesn't already have them.
I am new to Flutter and coding. I followed the guide here for how to add a watermark to an image. However, I am not using image picker, but using an image stored within Firebase, and a watermark that is an asset.
The code builds fine, but when I press the button to generate the watermarked image and eventually share it, I get the following error
Unhandled Exception: FileSystemException: Cannot open file, path = 'firebase url path' (OS Error: No such file or directory, errno = 2)
It is recognizing the path to the image in Firebase, but for some reason is saying the file isn't available. The error is being thrown on the 'decodeImage' portion of the code below.
Code snippet below
import '../backend/image_share/image_share.dart';
import 'package:image/image.dart' as ui;
import 'dart:io';
onPressed: () async {
//first image is a firebase path
final pickedFile = File('firebae path');
//second image is watermark and an asset
final watermark = File('assets/images/Share-small.png');
ui.Image originalImage = ui.decodeImage(pickedFile.readAsBytesSync());
ui.Image watermarkImage = ui.decodeImage(watermark.readAsBytesSync());
ui.drawImage(originalImage, watermarkImage);
ui.drawString(originalImage, ui.arial_24, 100, 120, 'Test!');
List<int> wmImage = ui.encodePng(originalImage);
final uploadUrl = await uploadData('new firebase data', wmImage);
final 'new firebase data' = FB collection(sharedImage: uploadUrl);
I am having trouble figuring out how to read/upload the image file before manipulating them.
The File class can only read/write files that are on the local system. It does not have any knowledge of files in Cloud Storage.
You will need to:
download the file from Cloud Storage,
add the watermark on the device, and then
write the resulting file back to Cloud Storage.
I'm trying to save a ByteData image generated from canvas into the device download folder. However, I was only able to save it into my apps android/data/ directory using the code below...
Future<void> saveImage(
String fileName,
ByteData image,
) async {
final directoryName = "Generated";
Directory directory = await getExternalStorageDirectory();
String path = directory.path;
await Directory('$path/$directoryName').create(recursive: true);
File('$path/$directoryName/$fileName.png').writeAsBytesSync(image.buffer.asInt8List());
}
but, it's not convenient for the user to go into this deep directory just to get the image, so I want to save it into the Download directory but I don't know how. I have tried several approaches but I keep on receiving permission errors. Like for example using this code below (downloads_path_provider)...
Directory directory = await DownloadsPathProvider.downloadsDirectory;
String path = directory.path;
File('$path/$fileName.png').writeAsBytesSync(image.buffer.asInt8List());
PS. It would be much better if there's a way to save it somewhere that will show in the Phone's Gallery
You can save images to gallery with this package on pub.dev : 'gallery_saver' : https://pub.dev/packages/gallery_saver
I am building an app in flutter and I want to store many images. So will anyone suggest me where I can store the images which is easy to use in my app. I mean should I store it locally or in cloud? If yes which cloud or backend should I use, whichone is good and fully optimized for my flutter app (like mongo, django, firebase etc. ). Will anyone suggest me the best?
Anyone kind of help is appreaciated as I have no prior knowledge about the production part....
Storing Images on a server can be very expensive, since the file sizes are very large compared to the usual data. So if you do not NEED to store them on a server, don't.
Storing images locally is pretty simple. You will want to use the path_provider package https://pub.dev/packages/path_provider . I ll post a function I am using in my current project that does this. You ll see, its pretty simple.
Note: In my Code I pull the file from my server. Obviously leave that part out if you are getting your images from a different source.
Future<File> createFileOfPdfUrl(String fileLocation, String name) async {
final url = Helper.baseUrl + "Files/Newsletter/" + fileLocation;
final filename = url.substring(url.lastIndexOf("/") + 1);
var request = await HttpClient().getUrl(Uri.parse(url));
var response = await request.close();
var bytes = await consolidateHttpClientResponseBytes(response);
String dir = (await pathProvider.getApplicationDocumentsDirectory()).path;
File file = new File('$dir/$filename');
await file.writeAsBytes(bytes);
return file;
}
I'm using the flutter camera package to record videos and save videos to a temporary directory after which I use flutter's ffmpeg package to do some transformation. However, to achieved this, I first had to make a copy of the recorded video to create the output file path.
The challenge comes in when I'm trying to load the asset from the device. The block of code below does the copying and renaming of the file.
static Future<File> copyFileAssets(String assetName, String localName) async {
ByteData assetByteData = await rootBundle.load(assetName);
final List<int> byteList = assetByteData.buffer
.asUint8List(assetByteData.offsetInBytes, assetByteData.lengthInBytes);
final String fullTemporaryPath =
join((await tempDirectory).path, localName);
return new File(fullTemporaryPath)
.writeAsBytes(byteList, mode: FileMode.writeOnly, flush: true);
}
The issue lies with this line ByteData assetByteData = await rootBundle.load(assetName);
I get this error message Unable to load asset: /storage/emulated/0/Android/data/com.timz/files/timz/1585820950555.mp4, but the weird thing is, this only happens when I run the build for the first. Everything else works fine on subsequent hot restarts.
I later got this fix by myself rootBundle is meant for loading only assets you've declared their paths on your pubspec.yaml but somehow, it miraculously loads the saved file when hot restart was applied.
Reading the file as bytes gave what I wanted as load it with root bundle. Here's the code below.
Uint8List assetByteData = await File(assetName).readAsBytes();