Search code examples
androidandroid-studiospeech-recognitionspeech-to-text

Saving the converted speech-to-text file/outcome into the external/internal storage


I'm new to Android development, and I want to know if its possible to save the converted speech-to-text files that's been converted through the Google Speech Recognition API?

To make it clear

  1. I'm developing an Android app which would let the user to record a speech
  2. Then would be converted into text, just like what the said API above exactly does.

But the app also has the gallery where the user may view the recorded speech and converted speech-to-text file by the said API. I'm in need of big help how would I implement the said process I wanna see as the outcome of my still-under-construction-application.

Here is the source code I'm using, and its from the internet (I'm not the one who created it):

package com.example.randallinho.saling_wika;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class RecordModule extends Activity {
protected static final int RESULT_SPEECH = 1;

private ImageButton btnSpeak;
private TextView txtText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recordmodule);

    txtText = (TextView) findViewById(R.id.txtText);

    btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

    btnSpeak.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent = new Intent(
                    RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

            try {
                startActivityForResult(intent, RESULT_SPEECH);
                txtText.setText("");
            } catch (ActivityNotFoundException a) {
                Toast t = Toast.makeText(getApplicationContext(),
                        "Opps! Your device doesn't support Speech to Text",
                        Toast.LENGTH_SHORT);
                t.show();
            }
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.recordmodule, menu);
    return true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case RESULT_SPEECH: {
            if (resultCode == RESULT_OK && null != data) {

                ArrayList<String> text = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                txtText.setText(text.get(0));
            }
            break;
        }

    }
}

Please excuse my disability of using the code format (I'm still in process of getting used to it).


Solution

  • try this code to write text file in android

    private void writeToSDFile(String speechToTextData){
    
        // Find the root of the external storage.
        // See http://developer.android.com/guide/topics/data/data-  storage.html#filesExternal
    
        File root = android.os.Environment.getExternalStorageDirectory(); 
    
        File dir = new File (root.getAbsolutePath() + "/folder");
        dir.mkdirs();
        File file = new File(dir, "text.txt");
        try {
            FileOutputStream f = new FileOutputStream(file);
            PrintWriter pw = new PrintWriter(f);
            pw.println(speechToTextData);
            pw.flush();
            pw.close();
            f.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Intent intent = new Intent(Intent.ACTION_EDIT);
            Uri uri = Uri.fromFile();
            intent.setDataAndType(uri, "plain/text");
            startActivity(intent);
        } catch(Exception ex) {
            Log.e("tag", "No file browser installed. " + ex.getMessage());
        }
    }
    

    Don't forget to add READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permission in AndroidManifest.xml file.

    reference from this question