1

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).

Sreehari
  • 5,411
  • 2
  • 21
  • 56
Jondy
  • 23
  • 6
  • i'm not getting what you actually want?? To store speech-to-text data into storage or to store user's speech to storage – ELITE Feb 01 '16 at 09:56
  • What I actually want is both. Its like this, I've got two libraries on my app and those are: 1.) Recorded Speech (the recorded speech by the API) 2.) Text Files (the converted file by the API) So its actually storing the output data into the user's device storage. – Jondy Feb 01 '16 at 09:59

2 Answers2

0

If you need to save text, numbers or data, there is a lot of ways:

  • Shared Preferences (Store private primitive data in key-value pairs)
  • Internal Storage (Store private data on the device memory)
  • External Storage (Store public data on the shared external storage)
  • SQLite Databases (Store structured data in a private database)
  • Network Connection (Store data on the web with your own network server)

Src: http://developer.android.com/guide/topics/data/data-storage.html

bNj06
  • 103
  • 10
0

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

Community
  • 1
  • 1
ELITE
  • 5,517
  • 3
  • 16
  • 24
  • Should I create this method under the method onCreate()? And I have another question if you wouldn't mind. How can I view the saved files in the external storage while using the app? – Jondy Feb 01 '16 at 10:34
  • you cannot declare one method inside another. so you have to create this method outside `onCreate()` of activity. And you can open file using any file browser. – ELITE Feb 01 '16 at 10:37
  • Alright, thanks for the info. By the way, would you mind if you tell me more info about file browsers? I've searched about it through google and I don't seem to understand it very well. Is it being called through methods or its another app should I use (because google gave me links where can I download apk of it)? – Jondy Feb 01 '16 at 10:43
  • I'm using `ES File Explorer`. Samsung mobile provides `My Files` application. Likewise you can find it on google playstore for more. – ELITE Feb 01 '16 at 10:54
  • Alright. I think that would be all. Thank you for your time, sir. – Jondy Feb 01 '16 at 10:55