Home > Enterprise >  Flutter: How to get asset path (video) for File class
Flutter: How to get asset path (video) for File class

Time:05-17

I saved a video in my asset folder and i am trying to get the video using the File class.

For example:

Final File video = File(String path);

So my question is where can i find the String path?

I already added this file to the pubspec.yaml

assets:
    - assets/mbf.mp4

Thank you

CodePudding user response:

You need copy file to device and then get path of file.

See exampl code:

void _copyAssetToLocal() async {
try {
  var content = await rootBundle.load("assets/intro.mp4");
  final directory = await getApplicationDocumentsDirectory();
  var file = File("${directory.path}/intro.mp4");
  file.writeAsBytesSync(content.buffer.asUint8List());
  loadIntroVideoMethod(file.path);
} catch (e) {
  
}}

CodePudding user response:

The path is assets/mbf.mp4, but it is not compatible with File constructor.

See Adding assets and images for details.

Using AssetBundle data can be loaded, which of course can be written to a file.

import 'package:flutter/services.dart' show rootBundle;

final byteData = await rootBundle.load('assets/mbf.mp4');
  • Related