保存经转换的语音到文本文件/结果到外部/内部存储 [英] Saving the converted speech-to-text file/outcome into the external/internal storage

查看:224
本文介绍了保存经转换的语音到文本文件/结果到外部/内部存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid的发展,我想知道,如果它可以保存一个已经通过谷歌语音识别API转换的转换后的语音到文本文件?_爱要清楚< BR>

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. 我开发一个Android应用程序这将让用户录制
    演讲

  2. 然后将被转换成文本,就像什么上面说的API究竟。结果

但是该应用也有其中用户可通过所述API查看所记录的语音和转换的语音到文本文件中的库。我需要很大的帮助我就如何实现上述过程中,我想看到​​作为我仍然在建的应用程序的结果。

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.

下面是源$ C ​​$ C我使用的,其从互联网上(我不是谁创造了​​它的):

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;
        }

    }
}

请原谅我使用code画幅的残疾(我仍然在习惯了的过程)。

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

推荐答案

试试这个code写的android系统中的文本文件

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());
    }
}

不要忘了添加 READ_EXTERNAL_STORAG​​E WRITE_EXTERNAL_STORAG​​E 的Andr​​oidManifest.xml <许可/ code>文件。

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

从<参考href=\"http://stackoverflow.com/questions/8330276/write-a-file-in-external-storage-in-android\">this问题

这篇关于保存经转换的语音到文本文件/结果到外部/内部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆