为什么 TextToSpeech synthesizeToFile 返回 -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方法返回-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,异常仍然发生。


那么,现在如何解决这个问题?


慕桂英4014372
浏览 266回答 1
1回答

慕盖茨4494581

我发现要知道我需要在 logcat 中查看-1返回值的原因:synthesizeToFileE/TextToSpeechService:由于异常 java.io.IOException:无法使用 /data/data/com.domain.my/files/_12345_test:打开失败:EACCES(权限被拒绝)现在,我必须知道为什么会出现此异常......
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java