Copying a file to/from external storage

2019-05-30 13:36发布

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.

2条回答
家丑人穷心不美
2楼-- · 2019-05-30 13:48

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 the 0 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 the TextToSpeech.OnUtteranceCompletedListener Interface in your activity and overriding the onUtteranceCompleted() 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

查看更多
淡お忘
3楼-- · 2019-05-30 14:03

have you tried this old school method?

FileInputStream fis = new FileInputStream(tempDestFile);
FileOutputStream fos = new FileOutputStream(permDestFile);
byte[] bites= new byte[1024];
int count = fis.read(bites);
while (  count != -1){
    fos.write(bites, 0, count);
    count = fis.read(bites);            
}

and make sure the WRITE_EXTERNAL_STORAGE permission is in your manifest.

查看更多
登录 后发表回答