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;