为了账号安全,请及时绑定邮箱和手机立即绑定

为什么 TextToSpeech synthesizeToFile 返回 -1?

为什么 TextToSpeech synthesizeToFile 返回 -1?

慕桂英4014372 2023-02-23 14:29:06
我正在尝试使用 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 回答

?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

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

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

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


查看完整回答
反对 回复 2023-02-23
  • 1 回答
  • 0 关注
  • 184 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信