Search code examples
javaandroidaudio

Why is Audio Saving to Storage/Emulated/0 when using getExternalStoragePublicDirectory() in android studio


I have code that saves a text to speech conversion into an audio file. I am using the getExternalStoragePublicDirectory(Environment.DirectoryDownloads), to make a new folder or save the audio file in the folder already. This means it should be accessible in google files right? However, it is not visible in google files, and it saves to storage/emulated/0, which to my knowledge is not accessible in google files.

If there are any errors in this code that can be fixed to move it to an accessible place in google files, please tell me!

Code:

private void createFile() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE_CODE);
        }
    }

    File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "Text To Speech Audio");
    if (!folder.exists()) {
        boolean isDirectoryCreated = folder.mkdirs();
        if (!isDirectoryCreated)
            Toast.makeText(getContext(), "Can't create directory to save the Audio", Toast.LENGTH_SHORT).show();
    }
    folder.mkdirs();
    mAudioFilename = folder.getAbsolutePath() + "/" + translateTo + "-" + languageCode + mUtteranceID + System.currentTimeMillis() + ".wav";
}

private void saveAudioToFile(String textToSpeak) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        speech.synthesizeToFile(textToSpeak, null, new File(mAudioFilename), mUtteranceID);
        Toast.makeText(getContext(), "Saved to " + mAudioFilename, Toast.LENGTH_LONG).show();
    } else {
        HashMap<String, String> hm = new HashMap();
        hm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, mUtteranceID);
        speech.synthesizeToFile(textToSpeak, hm, mAudioFilename);
        Toast.makeText(getContext(), "Saved to " + mAudioFilename, Toast.LENGTH_LONG).show();
    }
}

Solution

  • This is a working code for Android 14 (API 34): To use the code without write permissions for API34 and e.g. API 29, you should not choose Download as destination folder, but e.g. /storage/emulated/0/Android/media/(YOUR_PACKAGE). Thus

    tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {    
      @Override public void onInit(int status) {
         tts.setLanguage(Locale.US);  //or an other language
         int answer = tts.synthesizeToFile("Hello world", null, new File(getApplicationContext().getExternalMediaDirs()[0].getAbsolutePath()+"/HelloWorld.wav"), "");
         if (answer==0) {
           Toast.makeText(getApplicationContext(), "WAV file successful created and stored in /storage/emulated/0/Android/media/(YOUR_PACKAGE)/HelloWorld.wav.", Toast.LENGTH_LONG).show();
         } else {
           Toast.makeText(getApplicationContext(), "WAV file NOT created or stored.", Toast.LENGTH_LONG).show();
         }
       }
    }, "com.google.android.tts");  //I had no success without this line, except in the emulator. Must be one of your TTS engines.