I'm fairly new to Android and am having an writing to a certain file. What I'm trying to do is use the TextToSpeech.synthesizeToFile() method to write an audio file. This is working except for the fact that when the app exits the TextToSpeech engine automatically deletes the file it created. So I then tried to make a copy of the created file in another directory. To do this I use the copyFile() method of the org.apache.commons.io.FileUtils package. Using this method only results in a newly created file of 0 in size. I'm using Environment.getExternalStorageDirectory() to get the appropriate external storage directory. I have tried writing to an existing directory such as /mnt/sdcard/Ringtones/ as well as a newly created directory under /mnt/sdcard/...
Another thing I checked was the result of File.canWrite() on the destination file. This actually returns false, which may be a clue. But the File.setWritable() method does not seem to exist in order to change that.
I'm using code such as the following and I have not seen any exceptions being thrown
try {
File from = new File(tempDestFile);
File to = new File(permDestFile);
FileUtils.copyFile(from, to);
} catch(IOException e) {
Log.e(T, "Error copying file", e);
} catch(NullPointerException e) {
Log.e(T, "Error copying file", e);
}
Any ideas on what I might try? Nothing I have found thus far has helped me much on this.
Thanks! I really appreciate such a great resource for topics like this.
Aha, I finally got it. And of course it makes perfect sense.
I was trying to copy the file immediately after calling
[synthesizeToFile()]
[1], and so the file was not finished being sythesized/created. Hence the0
byte file size.What works is the way that it's explained [here][2]. Basically, just sending in a unique ID to
synthesizeToFile()
as one of the optional parameters. Then, implementing theTextToSpeech.OnUtteranceCompletedListener
Interface in your activity and overriding theonUtteranceCompleted()
callback method.onUtteranceCompleted()
is then of course called when the utterance is finished being synthesized. You can check for the unique id you passed in earlier to tell the difference between multiple utterances that you may be waiting for to finish.Pretty standard stuff, I guess but for some reason it evaded me for a few days.
Hopefully this can help out someone else who might be stuck.
[1]:
http://developer.android.com/reference/android/speech/tts/TextToSpeech.html#synthesizeToFile%28java.lang.String,%20java.util.HashMap%3Cjava.lang.String,%20java.lang.String%3E,%20java.lang.String%29
[2]:
http://developer.android.com/resources/articles/tts.html
have you tried this old school method?
and make sure the WRITE_EXTERNAL_STORAGE permission is in your manifest.