我正在编写一个程序,它将有一个用户列表,每个用户将有他们自己的照片,从一个在线来源。我正在成功下载数据,我正在努力格式化图片格式。它成功地保存了文件,但没有以可读的格式保存。
void FriendsListProcess::picRequesFinished(QNetworkReply *reply)
{
QByteArray data = reply->readAll();
if(reply->error())
{
qDebug() << reply->errorString();
return;
}
emit savePic(pic_name_path,data);
reply->deleteLater();
}
void FriendsListProcess::savePicToFile(QString file, QByteArray &pic_data)
{
qDebug() << "File name from write pic: " << file;
QFile f(file);
if(f.open(QIODevice::WriteOnly))
{
QDataStream out(&f);
out << pic_data;
f.close();
}
}当我试图打开保存的文件时,窗口显示
"Windows Photo Viewer can't open this picture because either Photo Viewer doesn't support this file format. or you don't have the lates updates to Photo Viewer"你们有什么建议吗?
发布于 2014-01-13 04:56:18
您的代码中存在以下问题:
ReadOnly的形式打开文件,而您想要编写它。我会写这样的东西:
void FriendsListProcess::picRequesFinished(QNetworkReply *reply)
{
QFile file(pic_name_path);
if (!file.open(QIODevice::WriteOnly)) {
qDebug() << "Failed to open the file for reading:" << file.fileName();
return;
}
// You would need to make sure you do not get irrelevant data in the meantime
while (reply->bytesAvailable()) {
if (file.write(reply->read(512)) == -1)
qDebug() << "Error while reading data:" << file.errorString();
}
}https://stackoverflow.com/questions/21083985
复制相似问题