简体   繁体   中英

why TextToSpeech synthesizeToFile returns -1?

I'm trying to create a file with 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);
        }
    }
}

With the device having Android 6 the synthesizeToFile method works fine.

With the device having Android 4.1 the synthesizeToFile method returns -1 .

I already checked with getEngines() that "com.google.android.tts" is installed.

How can I debug my script to discover why synthesizeToFile returns -1 ?

There is an alternative way to generate that file with TTS?

I need to do that in the internal storage (the Path returned by getFilesDir() ) so I must not ask for external storage permission.

EDIT:

In the logcat I found this error:

E/TextToSpeechService: Can't use /data/data/com.domain.my/files/_12345_test due to exception java.io.IOException: open failed: EACCES (Permission denied)

I already tried:

setWritable(true)

and

setWritable(true, true)

But even if both return true , the Exception still occurs.

So, now how to solve this?

I found that to know the reason of the value -1 returned by synthesizeToFile I need to see in the 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)

Now, I must know why this exception occurs...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM