我正在嘗試使用 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,異常仍然發生。那么,現在如何解決這個問題?
為什么 TextToSpeech synthesizeToFile 返回 -1?
慕桂英4014372
2023-02-23 14:29:06