9

I want to convert path "content://media/external/images/media/138501" to File and set in the Image.

Code:

File imageFile = File("content://media/external/images/media/138501");

is not working on:

DecorationImage(image: ExactAssetImage(imageFile.path),fit: BoxFit.fill)
Munish Thakur
  • 926
  • 1
  • 8
  • 25
  • The easiet way to do is to better convert the File to byte array and then show that using Image.memory. To convert the file to byte array follow this link (in Java) - https://stackoverflow.com/questions/10039672/android-how-to-read-file-in-bytes To show the byte array in flutter, follow this link - https://stackoverflow.com/questions/51516310/sending-bitmap-to-flutter-from-android-platform – Srinivas Valekar Dec 29 '19 at 21:26

6 Answers6

2

You can use uri_to_file package. It supports content:// URI.

Simple to use (Updated)

import 'package:uri_to_file/uri_to_file.dart';

try {
  String uriString = 'content://sample.txt';

  // 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);
} on UnsupportedError catch (e) {
  print(e.message);
} on IOException catch (e) {
  print(e);
} catch (e) {
  print(e);
}

then you can use this file as you want

Important note

Don't pass uri value like this

File file = await toFile(uri.toString());

Use like this if you are using uni_links package for deep linking purpose. Use getInitialLink() or linkStream

String? uriString = await getInitialLink();
if (uriString != null) {
  File file = await toFile(uriString);
}

linkStream.listen((uriString) async {
  if (uriString != null) {
    File file = await toFile(uriString!);
  }
});

This will not modify the uri string as we are not using uri.toString()

So this package will work fine

Working example: Working example

For more details: uri_to_file

Nikhil
  • 29
  • 5
  • worked for me, having trouble with flutter_absolute_path. This solved my problem. Thank you – LordZyx Oct 07 '21 at 13:18
  • Got error: Unable to fetch filename. – Promlert Lovichit Nov 29 '21 at 07:46
  • Hi @PromlertLovichit. "Got error: Unable to fetch filename" This issue is now fixed also updated the answer You can use latest uri_to_file: 0.2.0 Just make sure you pass the uri string value properly as given in the updated answer – Nikhil Dec 07 '21 at 20:10
1

According to the doc you can use the fromUri constructor: File.fromUri(Uri uri).

Your case:

File imageFile = File.fromUri("content://media/external/images/media/138501");
0

You can use Absolute path plugin.

final filePath = await FlutterAbsolutePath.getAbsolutePath(uriString);

and then show the image by using Image.file:

Image.file(
    File(filePath),
);
Saman Salehi
  • 1,995
  • 1
  • 22
  • 36
0

Make use of flutter_absolute_path package. flutter_absolute_path: ^1.0.6 In pubsec.yaml


To convert file path from this format : “content://media/external/images/media/5275” To "/storage/emulated/0/DCIM/Camera/IMG_00124.jpg”

final filePath = await FlutterAbsolutePath.getAbsolutePath("Any path 
   here");

File tempFile = File(filePath);

    if (tempFile.existsSync()) {
      //add your logic here.
   }
David Buck
  • 3,752
  • 35
  • 31
  • 35
Mohd Danish Khan
  • 1,044
  • 11
  • 12
0

define your URL:

final url = "Your Image Url";

then convert the Image Url to File in flutter :

XFile _xFile = XFile(url);

Done.

Louay Sleman
  • 1,794
  • 2
  • 15
  • 39
-1

You could use Image.file constructor.

DecorationImage(
  image: Image.file(File("content://media/external/images/media/138501")),
  fit: BoxFit.fill
)

Please take a note: On Android, this may require the android.permission.READ_EXTERNAL_STORAGE permission.

AgainPsychoX
  • 1,527
  • 1
  • 16
  • 20
  • I tried the same, But code did not work. I took "content://medi..." path from Asset.dart object and tried to convert into file. It seems flutter does not accept converting above URL directly to File. – Munish Thakur Jun 23 '19 at 12:51