简体   繁体   中英

permission denied for writing in the path returned by getFilesDir()

I need to create an audio file with synthesizeToFile .

It works on Android 6 (with the overloaded version of synthesizeToFile ) but in Android 4.1 synthesizeToFile returns -1 .

The synthesizeToFile official documentation says:

Synthesizes the given text to a file using the specified parameters. This method is asynchronous, ie the method just adds the request to the queue of TTS requests and then returns .

Then, to know which error caused that -1 I searched in the logcat where I founded this exception:

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

There is some different system configuration/setting between Android 6 and 4.1 which cause this error?

I must pass to synthesizeToFile a different path than the one returned by getFilesDir() ?

I must set file permissions?

Code I used for Android 4.1:

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.wav";

        File fileToGenerate = new File(completePathFile);
        String fileName = fileToGenerate.getName();

        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);
    }
}

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

I need to save the file in the internal storage so I must not ask for external storage permission (it is true also for Android 4.1? Or for this versione I need to do so?).

If I deliberately pass to synthesizeToFile a path that doesn't exists, the error in the logcat changes to file not found exception so that method checks correctly that the path completePathFile exists.

I came across your issue searching for a solution to my issue. Use of sound files in TTS on Marshmallow (Android 6) fails with permission issues

For me, it's addspeech() in TTS.

I think permission issues on TTS come from the fact that TTS in a device is a separate application and the TTS app doesn't have permissions to write or read files in the external or internal storage of your app.

It's funny that I call the instance of TTS in my app code and use all its functions but they can't access my app's storage. TTS is not my app, so there's no way I can request permissions on behalf of TTS. I think it's a kind of bug Google has to handle.

Anyway, I let the Google team know the issue in the link below. They didn't respond yet though. https://issuetracker.google.com/issues/152671139

You can support me in the link above, or you can post your own issue to let them know that there're multiple developers hoping TTS permission issues are taken care of.

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