How can I get a File from an ImageProvider?
ImageProvider imageProvider = NetworkImage(networkUrl);
File file = imageProvider ?
Although ImageProvider with NetworkImage renders the content of your network image URL, it doesn't have direct APIs or easy way for you to be able to convert it to a File object. With that said, you can still manually cache (or download) the image/s and get the download stream.
As far as I can understand your question, you are trying to access the network image URL as a File object. Instead of using ImageProvider, you can take a look at the flutter_cache_manager, which is a plugin used for downloading and caching files locally, and save it for later use.
Example Usage
Downloading network image file from URL
await DefaultCacheManager().downloadFile(url);
Retrieving File object from the cache dir
// Retrieving File object
var file = await DefaultCacheManager().getSingleFile(imageUrl);
// File object available for use
// Eg. Reading file as string
file.readAsStringSync(...);
Further reading
https://pub.dev/packages/flutter_cache_manager
https://pub.dev/packages/cached_network_image
Related
I am able to read the file as byte
final bytes = File("path....").readAsBytesSync();
//now, here is the problem
final doc = PdfDocument.load(parserBase);
★ that abstract class PdfDocumentParserBase(bytes) takes Uint8list as its argument which is also a parameter for
PdfDocument.load(parserBase)
, am not good at abstract classes and don't know how to use it in the method.
What I want to achieve is merging multiple pdf files.
Please help me out.
Thanks
I am using the flutter package camera_windows using the exact sample code listed here
camera_windows
It looks like by default it saves it to the "Pictures" directory in windows but I was wondering how do I save it to a different directory with a different filename also it looks like I cant pass a "path" to the call of the function for example
final XFile file = await CameraPlatform.instance.takePicture(_cameraId);
_showInSnackBar('Picture captured to: ${file.path}');
For example, my podcast app has a list of all downloaded podcast, how do I get a list of all LockCachingAudioSource that has been downloaded using request() method?
When you create your LockCachingAudioSource instances, you can choose the location where you want them to be saved. If you create a directory for that purpose, you can obtain a directory listing using Dart's file I/O API. The directory listing will also show partially downloaded files and other temporary files, which you want to ignore. These have extensions .mime and .part.
Having explained that, here is a solution. First, create your cache directory during app init:
final cacheDir = File('/your/choice/of/location');
...
await cacheDir.create(recursive: true);
Then for each audio source, create it like this:
import 'package:path/path.dart' as p;
...
source = LockCachingAudioSource(
uri,
cacheFile: File(p.joinAll([cacheDir, 'yourChoiceOfName.mp3'],
);
Now you can get a list of downloaded files at any time by listing the cacheDir and ignoring any temporary files:
final downloadedFiles = (await _getCacheDir()).list().where((f) =>
!['mime', 'part'].contains(f.path.replaceAll(RegExp(r'^.*\.'), '')));
If you need to turn these files back into the original URI, you could either create your own database to store which file is for which URI, or you choose the file name of each of your cache files by encoding the URI in base64 or something that's reversable, so given a file name, you can then decode it back into the original URI.
I am trying to get an image uploaded by user, store it in a folder(for which I am providing absolute path), store the path in database(Relative path) abd later use the path from database todisplay the image.
I am using cos-MultipartRequest jar file for this.
I don't want to have spaces in the file name(Since the image with spaces in name is not displayed by <img>)
The code I have written is:
String pathUPLOAD="D:/AdvJava/proimp/WebContent/images/default";
String pathDB="images/default";
MultipartRequest m=new MultipartRequest(request,pathUPLOAD);
String file=m.getFilesystemName("file").replaceAll("\\s+",""); //to remove spaces
m.getFile("file").renameTo(new File(pathDB+file)); // to rename the uploaded file without whitespaces
//(tried this earlier) m.getFile("file").renameTo(new File(file));
the path I am inserting in DB is: pathDB+"/"+file
The upload works fine and later the images are also displayed for those which don't have spaces in name.
But for those having spaces, the path stored in DB is the way I want, but the image uploaded in the specified directory does not has any changes in its name and therefore the place where images are to be displayed shows a missing image.
Any suggestions?
I have a project with a image stored as a logo that I wish to use.
URL logoPath = new MainApplication().getClass().getClassLoader().getResource("img/logo.jpg");
Using that method I get the URL for the file and convert it to string. I then have to substring that by 5 to get rid of this output "file:/C:/Users/Stephen/git/ILLA/PoC/bin/img/logo.jpg"
However when I export this as a jar and run it I run into trouble. The URL now reads /ILLA.jar!/ and my image is just blank. I have a gut feeling that it's tripping me up so how do I fix this?
Cheers
You are almost there.
Images in a jar are treated as resources. You need to refer to them using the classpath
Just use getClass().getResource: something like:
getClass().getResource("/images/logo.jpg"));
where "images" is a package inside the jar file, with the path as above
see the leading / in the call - this will help accessing the path correctly (using absolute instead of relative). Just make sure the path is correct
Also see:
How to includes all images in jar file using eclipse
See here: Create a file object from a resource path to an image in a jar file
String imgName = "/resources/images/image.jpg";
InputStream in = getClass().getResourceAsStream(imgName);
ImageIcon img = new ImageIcon(ImageIO.read(in));
Note it looks like you need to use a stream for a resource inside an archive.