为什么TextToSpeech synthesizeToFile返回-1?

时间:2019-06-28 12:30:53

标签: java android file debugging text-to-speech

我正在尝试使用synthesizeToFile创建文件:

TextToSpeech tts = new TextToSpeech(getApplicationContext(), this, "com.google.android.tts");

public void onInit(int status)
{
    if (status == TextToSpeech.SUCCESS)
    {
        String textToGenerate = "this is a test";
        // /data/data/com.domain.my/files is returned by getFilesDir()
        String completePathFile = "/data/data/com.domain.my/files/_12345_test";

        File fileToGenerate = new File(completePathFile);
        String fileName = fileToGenerate.getName();

        // this works on Android 6
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            Bundle bundleTts = new Bundle();
            bundleTts.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, fileName);

            tts.synthesizeToFile
            (
                    textToGenerate
                    , bundleTts
                    , fileToGenerate
                    , fileName
            );
        }
        // this doesn't works on Android 4.1: response is -1
        else
        {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, fileName);

            int response = tts.synthesizeToFile
            (
                    textToGenerate
                    , hashMap
                    , completePathFile
            );
            Log.d("testTTS", "Generation file " + fileName + " - response = " + response);
        }
    }
}

对于具有Android 6的设备,synthesizeToFile方法可以正常工作。

在具有Android 4.1的设备上,synthesizeToFile方法返回-1

我已经使用getEngines()检查是否已安装“ com.google.android.tts”。

如何调试脚本以发现为什么synthesizeToFile返回-1的原因?

还有使用TTS生成该文件的另一种方法吗?

我需要在内部存储(getFilesDir()返回的路径)中执行此操作,所以我一定不要请求外部存储许可。

编辑:

在logcat中,我发现了此错误:

E/TextToSpeechService: Can't use /data/data/com.domain.my/files/_12345_test due to exception java.io.IOException: open failed: EACCES (Permission denied)

我已经尝试过:

setWritable(true)

setWritable(true, true)

但是即使两者都返回true,仍然会发生异常。

那么,现在如何解决呢?

1 个答案:

答案 0 :(得分:0)

我发现要知道-1返回的值synthesizeToFile的原因,我需要在logcat中查看:

  

E / TextToSpeechService:由于异常java.io.IOException而无法使用/data/data/com.domain.my/files/_12345_test:打开失败: EACCES(权限被拒绝) < / p>

现在,我必须知道为什么会发生此异常...