[英]permission denied for writing in the path returned by getFilesDir()
我需要用synthesizeToFile創建一個音頻文件。
它適用於 Android 6(使用SynthesizeToFile的重載版本),但在 Android 4.1 中synthesizeToFile
返回-1
。
使用指定的參數將給定的文本合成到一個文件中。 此方法是異步的,即該方法只是將請求添加到 TTS 請求隊列中,然后返回。
然后,要知道是哪個錯誤導致了-1
我在創建此異常的 logcat 中進行了搜索:
E/TextToSpeechService:由於異常 java.io.IOException,無法使用 /data/data/com.domain.my/files/_12345_test.wav:打開失敗: EACCES(權限被拒絕)
Android 6 和 4.1 之間有一些不同的系統配置/設置會導致此錯誤嗎?
我必須將與getFilesDir()
返回的路徑不同的路徑傳遞給synthesizeToFile
嗎?
我必須設置文件權限嗎?
我用於 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);
}
}
我已經檢查了 getEngines() 是否安裝了“com.google.android.tts”。
我需要將文件保存在內部存儲中,所以我不能要求外部存儲許可(Android 4.1 也是如此?或者對於這個版本我需要這樣做?)。
如果我故意傳遞給synthesizeToFile
一個不存在的路徑,logcat 中的錯誤會更改為file not found exception
以便該方法正確檢查路徑completePathFile
存在。
我遇到了您的問題,正在尋找解決我的問題的方法。 在 Marshmallow (Android 6) 上的 TTS 中使用聲音文件失敗並出現權限問題
對我來說,它是 TTS 中的 addpeech()。
我認為 TTS 的權限問題來自這樣一個事實,即設備中的 TTS 是一個單獨的應用程序,並且 TTS 應用程序無權在應用程序的外部或內部存儲中寫入或讀取文件。
有趣的是,我在我的應用程序代碼中調用了 TTS 實例並使用了它的所有功能,但他們無法訪問我的應用程序的存儲空間。 TTS 不是我的應用程序,因此我無法代表 TTS 請求權限。 我認為這是谷歌必須處理的一種錯誤。
無論如何,我在下面的鏈接中讓 Google 團隊知道了這個問題。 不過他們還沒有回應。 https://issuetracker.google.com/issues/152671139
您可以在上面的鏈接中支持我,也可以發布您自己的問題,讓他們知道有多個開發人員希望解決 TTS 權限問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.