簡體   English   中英

為什么TextToSpeech synthesizeToFile返回-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方法返回-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 ,仍然會發生異常。

那么,現在如何解決呢?

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

E / TextToSpeechService:由於出現異常java.io.IOException而無法使用/data/data/com.domain.my/files/_12345_test:打開失敗: EACCES(權限被拒絕)

現在,我必須知道為什么會發生此異常...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM