Search code examples
androidtext-to-speech

tts files are not saved from api 29 or higher on Android


I am saving tts file using synthesizeToFile

However, after increasing compileSdkVersion and targetSdkVersion to 30, it does not work. The source code is below Thanks for reading

public void saveToFile()
{
    File folder = new File(Environment.getExternalStorageDirectory(),SAVE_FOLDER);
    if(!folder.exists())
    {
        folder.mkdir();
    }
    }

    long now = System.currentTimeMillis();
    Date day = new Date(now);
    SimpleDateFormat getCurrentTime = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault());
    String fileName = String.valueOf(getCurrentTime.format(day));

    File sd = Environment.getExternalStorageDirectory();
    String savePath = "//" + SAVE_FOLDER + "//" + fileName + ".wav";

    file = new File(sd,savePath);


    HashMap<String, String> myHashRender = new HashMap();
    myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, edtContents.getText().toString());

    int result = tts.synthesizeToFile(edtContents.getText().toString(),myHashRender,file.toString());

}

Solution

  • Environment.getExternalStorageDirectory()   
    

    This method was deprecated in API level 29. To improve user privacy, direct access to shared/external storage devices is deprecated. When an app targets Build.VERSION_CODES.Q, the path returned from this method is no longer directly accessible to apps. Read getExternalStorageDirectory

    You should try with

     File fileOBJ = new File(getExternalCacheDir().getAbsolutePath());
     File fileOBJParam = new File(getExternalCacheDir().getAbsolutePath(),"2nd Param");