将转换后的语音转文本 file/outcome 保存到 external/internal 存储中

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

我是 Android 开发的新手,我想知道是否可以保存通过 Google 语音识别 [=33] 转换的语音转文本文件=]?

说清楚

  1. 我正在开发一个 Android 应用程序,它可以让用户录制 演讲
  2. 然后会转换成文本,就像上面说的API一样。

但该应用程序也有图库,用户可以在其中查看上述 API 录制的语音和转换后的语音到文本文件。我需要很大的帮助,我将如何实施我希望看到的上述过程作为我仍在建设中的应用程序的结果。

这是我正在使用的源代码,它来自互联网(我不是创建它的人):

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

    }
}

请原谅我使用代码格式的障碍(我还在适应它)。

如果您需要保存文字、数字或数据,有很多种方法:

  • 共享首选项(将私有原始数据存储在键值对中)
  • 内部存储(在设备内存中存储私有数据)
  • 外部存储(在共享外部存储上存储 public 数据)
  • SQLite 数据库(在私有数据库中存储结构化数据)
  • 网络连接(使用您自己的网络服务器将数据存储在网络上)

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

尝试使用此代码在 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());
    }
}

不要忘记在 AndroidManifest.xml 文件中添加 READ_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE 权限。

参考自this问题