为什么 TextToSpeech synthesizeToFile returns -1?
why TextToSpeech synthesizeToFile returns -1?
我正在尝试使用 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
方法 returns -1
.
我已经使用 getEngines() 检查了 "com.google.android.tts" 是否已安装。
我如何调试我的脚本来发现为什么 synthesizeToFile
returns -1
?
有另一种方法可以使用 TTS 生成该文件吗?
我需要在内部存储中执行此操作(路径 return 由 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)
但是即使returntrue
,还是会出现Exception。
那么,现在如何解决这个问题?
我发现要知道 synthesizeToFile
返回值 -1
的原因,我需要在 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)
现在,我必须知道为什么会出现此异常...
我正在尝试使用 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
方法 returns -1
.
我已经使用 getEngines() 检查了 "com.google.android.tts" 是否已安装。
我如何调试我的脚本来发现为什么 synthesizeToFile
returns -1
?
有另一种方法可以使用 TTS 生成该文件吗?
我需要在内部存储中执行此操作(路径 return 由 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)
但是即使returntrue
,还是会出现Exception。
那么,现在如何解决这个问题?
我发现要知道 synthesizeToFile
返回值 -1
的原因,我需要在 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)
现在,我必须知道为什么会出现此异常...