1616package android .speech .tts ;
1717
1818import android .media .AudioFormat ;
19+ import android .os .FileUtils ;
1920import android .util .Log ;
2021
2122import java .io .File ;
22- import java .io .FileOutputStream ;
2323import java .io .IOException ;
2424import java .io .RandomAccessFile ;
2525import java .nio .ByteBuffer ;
@@ -63,7 +63,7 @@ void stop() {
6363 * Must be called while holding the monitor on {@link #mStateLock}.
6464 */
6565 private void cleanUp () {
66- closeFile ();
66+ closeFileAndWidenPermissions ();
6767 if (mFile != null ) {
6868 mFileName .delete ();
6969 }
@@ -72,7 +72,7 @@ private void cleanUp() {
7272 /**
7373 * Must be called while holding the monitor on {@link #mStateLock}.
7474 */
75- private void closeFile () {
75+ private void closeFileAndWidenPermissions () {
7676 try {
7777 if (mFile != null ) {
7878 mFile .close ();
@@ -81,6 +81,18 @@ private void closeFile() {
8181 } catch (IOException ex ) {
8282 Log .e (TAG , "Failed to close " + mFileName + ": " + ex );
8383 }
84+
85+ try {
86+ // Make the written file readable and writeable by everyone.
87+ // This allows the app that requested synthesis to read the file.
88+ //
89+ // Note that the directory this file was written must have already
90+ // been world writeable in order it to have been
91+ // written to in the first place.
92+ FileUtils .setPermissions (mFileName .getAbsolutePath (), 0666 , -1 , -1 ); //-rw-rw-rw
93+ } catch (SecurityException se ) {
94+ Log .e (TAG , "Security exception setting rw permissions on : " + mFileName );
95+ }
8496 }
8597
8698 @ Override
@@ -168,7 +180,7 @@ public int done() {
168180 int dataLength = (int ) (mFile .length () - WAV_HEADER_LENGTH );
169181 mFile .write (
170182 makeWavHeader (mSampleRateInHz , mAudioFormat , mChannelCount , dataLength ));
171- closeFile ();
183+ closeFileAndWidenPermissions ();
172184 mDone = true ;
173185 return TextToSpeech .SUCCESS ;
174186 } catch (IOException ex ) {
0 commit comments