7

I have an image saved on Firebase Storage. How can I assign this URL of the image file to a File?

Is there some way to do it as:

File file = File(URL);

I've also tried using http, but results in an error : 'FileSystemException was thrown resolving an image codec: Cannot open file'

final http.Response responseData = await http.get(url);
Uint8List uint8list = responseData.bodyBytes;
File file = File.fromRawPath(uint8list);

Using Uint8list has valid data, as seen when using widget shown below, but gives an error when used with above code, File.fromRawPath(unint8list)

Image.memory(uint8list);

Tried using ImageProvider but can't get a File object from it:

ImageProvider imageProvider = NetworkImage(url);
4
  • 1
    have you tried to access it from where its saved ? or why not display it directly using the download link ? Commented Jan 3, 2020 at 8:18
  • - It is saved on Firebase Storage, which has a URL. - I cannot display it using the URL because the widget has to also be able to get the file from local storage if required. Prefer having it as a file. Commented Jan 3, 2020 at 8:30
  • can you explain how you downloaded the image ? what method you have used ?and also you said it gives error, please post the error Commented Jan 3, 2020 at 8:39
  • As shown above I've used http, Commented Jan 3, 2020 at 8:43

5 Answers 5

12

Managed to get the File object, had to use Uint8List in a different way, so will use it for now, here is the working code:

final http.Response responseData = await http.get(strURL);
uint8list = responseData.bodyBytes;
var buffer = uint8list.buffer;
ByteData byteData = ByteData.view(buffer);
var tempDir = await getTemporaryDirectory();
File file = await File('${tempDir.path}/img').writeAsBytes(
    buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
Sign up to request clarification or add additional context in comments.

Comments

2

The following worked for me:

Future<File> urlToFile(String imageUrl) async {
  // generate random number.
  var rng = new math.Random();
  // get temporary directory of device.
  Directory tempDir = await getTemporaryDirectory();
  // get temporary path from temporary directory.
  String tempPath = tempDir.path;
  // create a new file in temporary path with random file name.
  File file = new File('$tempPath' + (rng.nextInt(100)).toString() + '.png');
  // call http.get method and pass imageurl into it to get response.
  http.Response response = await http.get(Uri.parse(imageUrl));
  // write bodybytes received in response to file.
  await file.writeAsBytes(response.bodyBytes);
  // now return the file which is created with random name in
  // temporary directory and image bytes from response is written to // that file.
  return file;
}

That's what I call it:

File? _image = await urlToFile('imageUrl');

based on: https://mrgulshanyadav.medium.com/convert-image-url-to-file-format-in-flutter-10421bccfd18

pdta: sorry for my English

Comments

1
import 'dart:io';

import 'package:uri_to_file/uri_to_file.dart';

Future<void> convertUriToFile() async {
  try {
    String uriString = 'content://sample.txt'; // Uri string

    // Don't pass uri parameter using [Uri] object via uri.toString().
    // Because uri.toString() changes the string to lowercase which causes this package to misbehave

    // If you are using uni_links package for deep linking purpose.
    // Pass the uri string using getInitialLink() or linkStream

    File file = await toFile(uriString); // Converting uri to file
  } on UnsupportedError catch (e) {
    print(e.message); // Unsupported error for uri not supported
  } on IOException catch (e) {
    print(e); // IOException for system error
  } catch (e) {
    print(e); // General exception
  }
}

Comments

0

The below source code easily worked for me,

import 'dart:math'; 

Future<File> convertUrlToFile(String imageUrl) async {

var rng = new Random();
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
File file = new File('$tempPath'+ (rng.nextInt(100)).toString() +'.png');
http.Response response = await http.get(Uri.parse(imageUrl));
await file.writeAsBytes(response.bodyBytes);
return file;
}

The above function can be called ,

File _image = await convertUrlToFile('imageUrl');

Comments

-5

try for example somthing like this

Image.network('https://cdn.mos.cms.futurecdn.net/LEkEkAKZQjXZkzadbHHsVj-970-80.jpg')

2 Comments

Your answer is inappropriate to question asked. your answer is how to show image using url in flutter, but question is how to get file from image url.
I think it is still relevant, as some people might only know how to display images as files. Therefore they might ask themselves how to convert URL to file. In that case, the answer is very helpful.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.