Here is my use case:
I am getting the list of URLs of images.
I want to display and cache them in the database, not only for a particular session so for the next time it should not do a web service call.
Our app is working offline as well. I tried a few libraries like flutter_advanced_networkimage and flutter_cache_manager but I'm getting quite lag and most of the times app crash.
Save it in your app's temp directory:
import 'dart:io';
// https://pub.dev/packages/path_provider
import 'package:path_provider/path_provider.dart';
final Directory temp = await getTemporaryDirectory();
final File imageFile = File('${temp.path}/images/someImageFile.png');
if (await imageFile.exists()) {
// Use the cached images if it exists
} else {
// Image doesn't exist in cache
await imageFile.create(recursive: true);
// Download the image and write to above file
...
}
It will persist through app launches and only gets deleted when the user personally clears the cache or reinstalls the app.
I've been using cached_network_image, works as advertised
Related
I am trying to build an app where the user can add images from Image Picker, then save it in a Provider as well as in Firebase. For retrieving it should be the other way around: Get it from Firebase and then store it in my Provider.
(ignore the Firebase part, for now I only want to store the images in a Provider, but it should be correctly set up, so I can add the Firebase support later)
The problem is that I am quite new to Flutter and I am not how to start here, because there are so many ways to load images in Flutter. From the ImagePicker I retrieve a File. What is the correct way to store this in a Provider so I can load it in another screen?
I know this is quite a genreal question but I hope some one can shed some light in the dark for me here :D Let me know if you need any more info.
I already setup a ChangeNotifier:
class MemoryImageProvider extends ChangeNotifier {
String path = '';
setImage(String path) {
path = path;
notifyListeners();
}
}
I trying to create a whatsapp status saver in flutter. I trying to save the whatsapp status. I created a folder statuses in /storage/emulated/0/statuses/ and The process of copying goes well. But that Image is not shown in the gallery app.
So I even Tried storing it in DCIM/Camera still it doesn't show up there. But when the copy the same image using file explorer then that image shows up in gallery app. I think something is wrong with the image properties.
The code used to save is here.
saveFile(filePath) async {
String newFilename;
File originalFile = File(filePath);
Directory _directory = await getExternalStorageDirectory();
if (!Directory("/storage/emulated/0/statuses/").existsSync()) {
Directory("/storage/emulated/0/statuses/")
.createSync(recursive: false);
}
String curDate = DateTime.now().toString();
newFilename = "/storage/emulated/0/statuses/VIDEO-$curDate.jpg";
await originalFile.copy(newFilename).then((value) {
print("Saved: " + newFilename);
});
}
When you change a media, you should tell the device to re-scan. This image saver plugin will notify the Android OS about the media that is saved.
If you don't want the plugin, you can use platform channels to write native code yourself.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
This issue is discussed here and here.
I have Flutter app which needs to load dynamic assets and content which I want to save for later use. I know about Assets I can have in build time at the folder "assets/" inside the app.
I want to download content using ZIP files and unzip them to app local folder so they won't delete in the next app update.
what are the folders Flutter allows me to add assets to at runtime?
You cannot dynamically add assets to Flutter app at runtime and that is why Shared_Preferences package was developed by the official Flutter_Dev Team.
https://pub.dev/packages/shared_preferences
If you want to store a File instead of bits of information then refer to the below example code (For a Image File):
Future getImage(ImageSource imageSource) async {
// using your method of getting an image
final File image = await ImagePicker.pickImage(source: imageSource);
// getting a directory path for saving
final String path = await getApplicationDocumentsDirectory().path;
// copy the file to a new path
final File newImage = await image.copy('$path/image1.png');
setState(() {
_image = newImage;
});}
If your still want to somehow add/delete files dynamically then the answer is that it is practically not possible because assets weren't designed to dynamically store files.
One should only use assets to store files which shall remain common for all users.
In chrome when you install a extension it clears the local storage associated with that extension. This doesn't seem to be the case for Firefox.
The only possible solution I can find is
https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials/Listening_for_load_and_unload
To listen for the 'install' event and clear it manually.
The problem Im having with this is that at the time main.js gets this event, my page-workers are already unloaded. I need to clear local storage which main.js cannot. So I need to emit to the page-worker so it can do so but the page-worker is already unloaded at this time.
The Firefox add-on SDK doesn't integrate with HTML5 local storage. So one solution is to use message passing and simple storage instead, then it should be wiped for you, as expected.
If it isn't, in main.js, you can simply write:
const { storage } = require('sdk/simple-storage');
exports.main = function({ loadReason }) {
if (loadReason==='install') for (var prop in storage) delete storage[prop];
}
If you need to use localStorage, store an array of affected pages in simple storage, then create a page-worker for each site on install and clear localStorage from each new content script.
I'm developing an app for Windows Phone 7 and I'm using a Phonegap template for it.
Everything looks perfect, but now I’m stuck trying to open a PDF file in the browser.
I tried the following but that doesn’t work because the url of the PDF exceeds the 2048 character limit (it’s a data url). This code runs after the deviceReady event was fired.
var ref = window.open('http://www.google.com', '_blank', 'location=no');
ref.addEventListener('loadstart', function () { alert(event.url); });
Now, I'm trying to save the PDF file to storage and then I'm trying to have it opened by the browser, but the browser doesn't show anything. I'm editing the InAppBrowser.cs code from cordovalib and I added the following lines before calling browser.Navigate(loc);
private void ShowInAppBrowser(string url)
{
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
FileStream stream = store.OpenFile("test.pdf", FileMode.Create);
BinaryWriter writer = new BinaryWriter(stream);
var myvar = Base64Decode("the big data url");
writer.Write(myvar);
writer.Close();
if (store.FileExists("test.pdf")) // Check if file exists
{
Uri loc = new Uri("test.pdf", UriKind.Relative);
...
}
}
This code is returning the following error:
Log:"Error in error callback: InAppBrowser1921408518 = TypeError: Unable to get value of the property 'url': object is null or undefined"
I don’t wanna use ComponentOne.
Any help would be greatly appreciated!
You cannot open pdf files from the isolated storage in the default reader for PDF files. If the file is online e.g. it has a URI for it, you can use WebBrowserTask to open it since that will download and open the file in Adobe Reader.
On Windows Phone 8 you actually can open your own file in default file reader for that extension, but I am not sure how that will help you since you target PhoneGap and Windows Phone 7.
Toni is correct. You could go and try to build your own viewer (which would be the same thing as using C1, but with more time involved). I worked on a port of iTextSharp and PDFSharp for WP7, but neither of which are PDF Viewers. They are good for creating PDFs and parsing them some (but to render them there is more work involved). This has been a personal quest of mine, but honestly the best I have gotten is to be able to extract some images from the PDF (and none of the text)
try this
var installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
var assets = await installedLocation.GetFolderAsync("Assets");
var pdf = await assets.GetFileAsync("metro.pdf");
Windows.System.Launcher.LaunchFileAsync(pdf);
This worked correctly on my Device.