复制一个文件到/从外部存储文件

2023-09-07 09:11:55 作者:完美勇士@

我是相当新的Andr​​oid和我有一个写入某个文件。我正在试图做的是使用TextToSpeech.synthesizeToFile()方法来写一个音频文件。这是工作除了以下事实:当该应用退出文字转语音引擎自动删除它创建的文件。于是,我又试图使创建的文件的副本复制到另一个目录中。要做到这一点我用org.apache.commons.io.FileUtils包copyFile()方法。使用这种方法仅导致0在大小新创建的文件。我使用Environment.getExternalStorageDirectory()来获取相应的外部存储目录。我试图写现有的目录如/ mnt / SD卡/铃声/以及新创建下的/ mnt / SD卡/...

我检查的另一件事是在目标文件File.canWrite()的结果。这实际上返回false,这可能是一个线索。但File.setWritable()方法似乎并不为了改变这种状况存在的。

我使用code,如下面的,我还没有看到被抛出的异常

  {尝试    从=新的文件(tempDestFile)文件;    文件以=新的文件(permDestFile);    FileUtils.copyFile(从,到);}赶上(IOException异常五){    Log.e(T,错误复制文件,E);}赶上(NullPointerException异常五){    Log.e(T,错误复制文件,E);} 

什么我可以尝试任何想法?没有什么迄今为止,我发现已经帮助了我很多这一点。

谢谢!我真的AP preciate例如像这样的主题一个很好的资源。

解决方案

哈哈,我终于得到它。当然它非常有意义。

我试图打电话后立即将文件复制 [synthesizeToFile()] [1],因此该文件未完成被sythesized /创建。因此, 0 字节的文件大小。

什么工作原理是,它的解释[这里]的方式[2]。基本上,只要发送一个唯一的ID synthesizeToFile()作为可选参数之一。然后,实施你的活动 TextToSpeech.OnUtteranceCompletedListener 接口并重写 onUtteranceCompleted()回调方法。

onUtteranceCompleted()时,话语完成正在合成,然后当然调用。您可以检查您在早些时候通过告诉你可能会等待到完成多个话语之间的差异的唯一ID。

pretty标准的东西,我猜,但由于某种原因,它回避了我几天。

希望这可以帮助了别人谁可能被卡住。

[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

我在从word文档中复制图片粘贴到桌面上保存了一个东西后电脑图标被蓝色笼罩怎么办

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 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