Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 826
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Rogers
Accepted Solution

naming saved files

I recently implemented a screen capture function in my app, but it currently only saves 1 image & over-writes it each time

 

.title("Shar	QImage image = QImage::fromData(array, "BMP");
	QFile outFile("shared/photos/TestImage.jpeg");
	outFile.open(QIODevice::WriteOnly);
	image.save(&outFile, "JPEG");

 is it possible to save it under a different name each time

ex: TestImage01.jpeg, TextImage02.jpeg

Developer
Posts: 16,842
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: naming saved files

well, you define the name yourself, so you should be able to use another one pretty easily.
Maybe list the files that exist on startup,find the last used number and count up from there.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 826
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Rogers

Re: naming saved files

I know i can change the name at any time, but that's only going to continue creating just the 1 file, what i don't understand is how to append a different # to each new image

QFile outFile("shared/photos/TestImage" + "01" + ".jpeg");

that should output TestImage01.jpeg but that's all it will output each time, how can i get the 01 to start counting up after each capture

 

 

Developer
Posts: 196
Registered: ‎03-04-2013
My Device: BB Z10
My Carrier: Rogers

Re: naming saved files

[ Edited ]

Well, you could use a variable and increment it everytime (count = 1....count++) then concatenate it to the filename etc etc..

 

--Edit--

Then again, I'd go with simon_hain's solution of checking existing files and incrementing as such since it would still overwrite any existing files each time the app is restarted...

 

Something like...

 

app startup

initialize counter = 1...

set fileName (concatenate with counter)...

while (fileExists)...

.....change fileName (concatenate with ++counter)

....

 

Then in your other screen capture area...

use current fileName to work with...

..... save the file...

set fileName (concatenate with ++counter)

 

Or something like that...

Developer
Posts: 826
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Rogers

Re: naming saved files

[ Edited ]

This allows me to insert a number but it will only (de)increment it by 1, once

 

	char buffer [50];
	for(int i = 0; i < 10; i++)
	{
		sprintf (buffer, "shared/photos/image%d.jpeg", i);
	}

	QFile outFile(buffer);

 the file would be saved as image9.jpeg, but it doesnt continue count down to image8.jpeg etc

 

if i reverse it i can have it go from 0+ except it still only goes up by a maximum of 1 & i'm not sure why it's stopping there but once i can get past that simply saving the images from 1 to ~ would be perfect

Developer
Posts: 196
Registered: ‎03-04-2013
My Device: BB Z10
My Carrier: Rogers

Re: naming saved files

[ Edited ]

You're getting there, but not quite.  Basically, buffer will have the last string that you give it in that loop, which is as you said 9... (The same goes for the reverse.. buffer has the last string which would be 1 (i > 0))...

 

More like:

 

int fileCounter = 1; // Global declaration somewhere

sprintf(buffer, "shared/photos/image%d.jpeg", fileCounter++);
QImage image = QImage::fromData(array, "BMP");
QFile outFile(buffer);
outFile.open(QIODevice::WriteOnly);
image.save(&outFile, "JPEG");

 

So each time that chunk of code is passed through the fileCounter will be incremented once it's used. So, for instance, the 2nd run through of that code, fileCounter == 2... 3rd run through fileCounter == 3.... etc

Developer
Posts: 826
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Rogers

Re: naming saved files

[ Edited ]

I gave that a try but it is only outputting image1.jpeg, it doesnt seem to increment the value of fileCounter at all

Developer
Posts: 196
Registered: ‎03-04-2013
My Device: BB Z10
My Carrier: Rogers

Re: naming saved files

Most likely you're declaring fileCounter right where I put it...  Put that declaration somewhere that will make it a global variable.. 

Highlighted
Developer
Posts: 826
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Rogers

Re: naming saved files

I sure was, got that changed & it works like a charm

Thanks Smiley Happy