简体   繁体   English

在 getFilesDir() 返回的路径中写入的权限被拒绝

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

I need to create an audio file with synthesizeToFile .我需要用synthesizeToFile创建一个音频文件。

It works on Android 6 (with the overloaded version of synthesizeToFile ) but in Android 4.1 synthesizeToFile returns -1 .它适用于 Android 6(使用SynthesizeToFile的重载版本),但在 Android 4.1 中synthesizeToFile返回-1

The synthesizeToFile official documentation says: synthesizeToFile官方文档说:

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 .此方法是异步的,即该方法只是将请求添加到 TTS 请求队列中,然后返回

Then, to know which error caused that -1 I searched in the logcat where I founded this exception:然后,要知道是哪个错误导致了-1我在创建此异常的 logcat 中进行了搜索:

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) E/TextToSpeechService:由于异常 java.io.IOException,无法使用 /data/data/com.domain.my/files/_12345_test.wav:打开失败: EACCES(权限被拒绝)

There is some different system configuration/setting between Android 6 and 4.1 which cause this error? Android 6 和 4.1 之间有一些不同的系统配置/设置会导致此错误吗?

I must pass to synthesizeToFile a different path than the one returned by getFilesDir() ?我必须将与getFilesDir()返回的路径不同的路径传递给synthesizeToFile吗?

I must set file permissions?我必须设置文件权限吗?

Code I used for Android 4.1:我用于 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.我已经检查了 getEngines() 是否安装了“com.google.android.tts”。

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?).我需要将文件保存在内部存储中,所以我不能要求外部存储许可(Android 4.1 也是如此?或者对于这个版本我需要这样做?)。

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.如果我故意传递给synthesizeToFile一个不存在的路径,logcat 中的错误会更改为file not found exception以便该方法正确检查路径completePathFile存在。

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 在 Marshmallow (Android 6) 上的 TTS 中使用声音文件失败并出现权限问题

For me, it's addspeech() in TTS.对我来说,它是 TTS 中的 addpeech()。

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.我认为 TTS 的权限问题来自这样一个事实,即设备中的 TTS 是一个单独的应用程序,并且 TTS 应用程序无权在应用程序的外部或内部存储中写入或读取文件。

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 实例并使用了它的所有功能,但他们无法访问我的应用程序的存储空间。 TTS is not my app, so there's no way I can request permissions on behalf of TTS. TTS 不是我的应用程序,因此我无法代表 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.无论如何,我在下面的链接中让 Google 团队知道了这个问题。 They didn't respond yet though.不过他们还没有回应。 https://issuetracker.google.com/issues/152671139 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.您可以在上面的链接中支持我,也可以发布您自己的问题,让他们知道有多个开发人员希望解决 TTS 权限问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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