0
votes

i wanna delete/remove a file from storage. the file is store in the "/shared/photos/". this is how i store the file

QByteArray* data; //some image data
QImage image;
image.loadFromData(*data);
QFile outFile("shared/photos/"+filename);
outFile.open(QIODevice::WriteOnly);
image.save(&outFile, "PNG");

and i can successfully view the image file with this code :

QString filepath;
QString workingDir = QDir::currentPath();
filepath = "file://" + workingDir + "/shared/photos/"+filename;

and it's viewed with no problem.

the QString "filepath" contains this string

"file:///accounts/1000/appdata/com.example.Project.testDev_le_Project4b5f4904/shared/photos/02.jpg"

And now i tried to delete/remove this file from storage. this is how i tried :

QString thumbnailImage = filepath;
// basically it contains string like filepath
//"file:///accounts/1000/appdata/com.example.Project.testDev_le_Project4b5f4904/shared/photos/02.jpg"
QFile thumb(thumbnailImage); 
bool ok = thumb.remove(); 
QString error = thumb.errorString(); 
if(ok){ qDebug() << "delete thumbnailImage success = " << ok; }
else{ qDebug() << "delete thumbnailImage failed !! "; }

and it's not working. the debug says "No such file or directory".

i also tried 
QFile::remove(thumbnailImage);

and still not working. i also tried :

QFile::remove("/shared/photos/"+filename);

but still not working. i also tried changing the workdir from QDir::currentPath() to QDir::homepath() and still no success.

so please tell me what exactly i should put in the QFile::remove() parameter. the reference https://developer.blackberry.com/native/reference/cascades/qfile.html#remove said the parameter is QString filename.

bool QFile::remove ( const QString & fileName )

what exactly i should insert the parameter? please help me guys.

thanks.

Regards, Yoga Try Utomo

1

1 Answers

0
votes

The file path is wrong. It should not contain "file://". Furthermore, you have to open the file before removing it.

QFile thumb("shared/photos/" + filename);
thumb.open(QIODevice::ReadWrite);
thumb.remove();
thumb.close();