I am using the QImageReader to read in all of the images in a directory. However it is sometimes unable to read certain images, even if it has read them successfully in the past. The error message is "Unable to read image data". Once there is an error on one file in a directory there tends to be an error on all subsequent files. Can someone explain what is going on? Here is my code:
filters += "*." + format;
dir.
setSorting(QDir::Time |
QDir::Reversed);
foreach
(QFileInfo fileInfo, dir.
entryInfoList(filters,
QDir::Files)) {
QString filename
= fileInfo.
fileName();
QString pathname
= fileInfo.
absoluteFilePath();
if (!imageReader)
{
}
else
{
imageReader->setFileName(pathname);
}
QImage image
= imageReader
->read
();
if (!image.isNull())
{
Image* myImage = new Image(image);
myImage->s_pathname(pathname);
myImage->s_timestamp (fileInfo.lastModified());
QList<Image *> *imglist = g_imageList();
imglist->append(myImage);
}
else
{
/*
** Sometimes get the error "Unable to read image data" which is the
** "InvalidDataError". But it is inconsistent. The same file is
** successfully read on other occasions.
** When it fails with one file tends to fail with all other files from that
** directory. (filename is always ok).
*/
QString errstr
= imageReader
->errorString
();
QString path
= imageReader
->fileName
();
}
}
QStringList filters;
foreach (QByteArray format, QImageReader::supportedImageFormats())
filters += "*." + format;
dir.setSorting(QDir::Time | QDir::Reversed);
foreach (QFileInfo fileInfo, dir.entryInfoList(filters, QDir::Files))
{
QString filename = fileInfo.fileName();
QString pathname = fileInfo.absoluteFilePath();
if (!imageReader)
{
imageReader = new QImageReader(pathname);
}
else
{
imageReader->setFileName(pathname);
}
QImage image = imageReader->read();
if (!image.isNull())
{
Image* myImage = new Image(image);
myImage->s_pathname(pathname);
myImage->s_timestamp (fileInfo.lastModified());
QList<Image *> *imglist = g_imageList();
imglist->append(myImage);
}
else
{
/*
** Sometimes get the error "Unable to read image data" which is the
** "InvalidDataError". But it is inconsistent. The same file is
** successfully read on other occasions.
** When it fails with one file tends to fail with all other files from that
** directory. (filename is always ok).
*/
QString errstr = imageReader->errorString();
QString path = imageReader->fileName();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks