From 381612f7944b202c8b1428f0cc9d1af72f5f7647 Mon Sep 17 00:00:00 2001 From: Assam Boudjelthia Date: Tue, 29 Aug 2023 10:34:53 +0300 Subject: [PATCH] Image Gestures Example: use QFileInfo for images file names The example keeps around image file names only and append them to the selected dir path, that works fine for file scheme files, but for Android with content scheme files, that doesn't work as good because usually the paths are returned by a provider and managing them manually like appending a file name to a directory (tree) path might not work. This patch retrieves QFileInfo objects and use the absolute file paths to open any image. Pick-to: 6.6 6.5 6.2 Fixes: QTBUG-116181 Change-Id: I9911a181d92ba0452500398cbe052b9583bd79a0 Reviewed-by: Friedemann Kleint Reviewed-by: Shawn Rutledge --- .../gestures/imagegestures/imagewidget.cpp | 22 +++++++++++++------ .../gestures/imagegestures/imagewidget.h | 5 +++-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/examples/widgets/gestures/imagegestures/imagewidget.cpp b/examples/widgets/gestures/imagegestures/imagewidget.cpp index e17bf3c0c0..3ee72392fd 100644 --- a/examples/widgets/gestures/imagegestures/imagewidget.cpp +++ b/examples/widgets/gestures/imagegestures/imagewidget.cpp @@ -149,15 +149,23 @@ void ImageWidget::openDirectory(const QString &path) this->path = path; QDir dir(path); const QStringList nameFilters{"*.jpg", "*.png"}; - files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name); + files = dir.entryInfoList(nameFilters, QDir::Files|QDir::Readable, QDir::Name); position = 0; goToImage(0); update(); } -QImage ImageWidget::loadImage(const QString &fileName) const +/* + With Android's content scheme paths, it might not be possible to simply + append a file name to the chosen directory path to be able to open the image, + because usually paths are returned by an Android file provider and handling those + paths manually is not guaranteed to work. For that reason, it's better to keep + around QFileInfo objects and use absoluteFilePath(). +*/ +QImage ImageWidget::loadImage(const QFileInfo &fileInfo) const { + const QString fileName = fileInfo.absoluteFilePath(); QImageReader reader(fileName); reader.setAutoTransform(true); qCDebug(lcExample) << "loading" << QDir::toNativeSeparators(fileName) << position << '/' << files.size(); @@ -187,7 +195,7 @@ void ImageWidget::goNextImage() prevImage = currentImage; currentImage = nextImage; if (position+1 < files.size()) - nextImage = loadImage(path + QLatin1Char('/') + files.at(position+1)); + nextImage = loadImage(files.at(position + 1)); else nextImage = QImage(); } @@ -204,7 +212,7 @@ void ImageWidget::goPrevImage() nextImage = currentImage; currentImage = prevImage; if (position > 0) - prevImage = loadImage(path + QLatin1Char('/') + files.at(position-1)); + prevImage = loadImage(files.at(position - 1)); else prevImage = QImage(); } @@ -234,12 +242,12 @@ void ImageWidget::goToImage(int index) position = index; if (index > 0) - prevImage = loadImage(path + QLatin1Char('/') + files.at(position-1)); + prevImage = loadImage(files.at(position - 1)); else prevImage = QImage(); - currentImage = loadImage(path + QLatin1Char('/') + files.at(position)); + currentImage = loadImage(files.at(position)); if (position+1 < files.size()) - nextImage = loadImage(path + QLatin1Char('/') + files.at(position+1)); + nextImage = loadImage(files.at(position + 1)); else nextImage = QImage(); update(); diff --git a/examples/widgets/gestures/imagegestures/imagewidget.h b/examples/widgets/gestures/imagegestures/imagewidget.h index 7e8142442a..b5bbc9ef0f 100644 --- a/examples/widgets/gestures/imagegestures/imagewidget.h +++ b/examples/widgets/gestures/imagegestures/imagewidget.h @@ -4,6 +4,7 @@ #ifndef IMAGEWIDGET_H #define IMAGEWIDGET_H +#include #include #include #include @@ -40,14 +41,14 @@ private: void swipeTriggered(QSwipeGesture*); //! [class definition begin] - QImage loadImage(const QString &fileName) const; + QImage loadImage(const QFileInfo &fileInfo) const; void loadImage(); void goNextImage(); void goPrevImage(); void goToImage(int index); QString path; - QStringList files; + QFileInfoList files; int position; QImage prevImage, nextImage;