[PROBLEM] How to store an image to reuse it
-
Hi everybody. Im developing an app that needs to download an image from internet and show it into an Image item inserted into a stackview. When I do a push I pass the Image url and in the next view pushed the image appear after 100-300 milliseconds because the Image {} redownlod it again. This is bad for my app, I need to save the image downloaded the first time and reuse it in the view pushed it instead of redownload it again. How can it be accomplish this behaviour?
Maybe the image is cached internally on my app somewhere?Thank you
-
Download the image first and display the local copy.
There are many ways to do this, for example you can use ImageProvider, or some custom caching class written in C++ which would add image(s) at known locations so that QML can load it.
-
Hey, thanks for the reply. I look into imageProvider but it seems pretty "not fast" to be implemented. I check the example but not super sure how it works, Im trying right now with some images.
There isn't something like the QSettings? That store data in predefined location for both Android and iOS and I dont even have to know the location?
Thank you -
Storing image into
QSettings
isn't a good idea. You can cache image in application's 'sandbox' and useQStandardPaths
for crossplatform solution. -
@IntruderExcluder said in [PROBLEM] How to store an image to reuse it:
For example:
QStandardPaths::PicturesLocation -
@IntruderExcluder Would you mind to give me an example or link on how to store image in the sandbox?
The image will live only when app is running because everytime I open it up it will download the images. But it seems a bit tricky. I already saw QStandardPaths and it is perfect for the store location -
@JoeCFD I found the solution now the problem is writing on Android, after downloading all the stuff and I try to write I get this error
"Problem opening save file "/storage/emulated/0/Pictures/tmpImg.jpg" for download "Operation not permitted"
Meanwhile If i create the file I get
"Problem opening save file "/storage/emulated/0/Pictures/tmpImg.jpg" for download "Permission Denied"
This is the code I used:void DownloadManager::startDownload() { if (downloadQueue.isEmpty()) { qDebug() << "[DownloadManager::startNextDownload]" << downloadedCount << " " << totalCount << " files download succesfully"; emit finished(); return; } QUrl url = downloadQueue.dequeue(); // QString filename = saveFileName(url); QString filename = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); //Prendo nome primo elemento ed inserisco nella lista, poi lo rimuovo if(!imagesName.isEmpty()) { filename.append("/"); filename.append(imagesName.first()); imagesName.removeFirst(); } qDebug() << "[DownloadManager::startNextDownload] Filename " << filename; output.setFileName(filename); if (!output.open(QIODevice::WriteOnly)) { qDebug() << "[DownloadManager::startNextDownload] Problem opening save file " << filename << " for download " << output.errorString(); qDebug() << "[DownloadManager::startNextDownload] Skipping download"; startNextDownload(); return; // skip this download } //Download the file into the location QNetworkRequest request(url); currentDownload = manager.get(request); connect(currentDownload, &QNetworkReply::finished, this, &DownloadManager::downloadFinished); connect(currentDownload, &QNetworkReply::readyRead, this, &DownloadManager::downloadReadyRead); // prepare the output qDebug() << "[DownloadManager::startNextDownload] Downloading " << url; }