【问题标题】:Saving the converted speech-to-text file/outcome into the external/internal storage将转换后的语音转文本文件/结果保存到外部/内部存储中
【发布时间】:2016-02-01 18:41:21
【问题描述】:

我是 Android 开发的新手,我想知道是否可以保存已通过 Google Speech Recognition API 转换的转换后的语音到文本的文件?

说清楚

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

    }
}

请原谅我无法使用代码格式(我还在适应它)。

【问题讨论】:

  • 我没有得到你真正想要的东西??将语音到文本的数据存储到存储中或将用户的语音存储到存储中
  • 我真正想要的是两者。就像这样,我的应用程序上有两个库,它们是:1.)录制的语音(API 录制的语音)2.)文本文件(API 转换的文件)所以它实际上存储了输出数据到用户的设备存储中。

标签: android android-studio speech-recognition speech-to-text


【解决方案1】:

尝试使用此代码在 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问题的参考

【讨论】:

  • 我应该在方法onCreate()下创建这个方法吗?如果你不介意,我还有一个问题。使用应用时如何查看外部存储中保存的文件?
  • 你不能在另一个方法中声明一个方法。所以你必须在onCreate() 的活动之外创建这个方法。您可以使用任何文件浏览器打开文件。
  • 好的,谢谢你的信息。顺便说一句,您介意告诉我有关文件浏览器的更多信息吗?我通过谷歌搜索过它,我似乎不太了解它。它是通过方法调用的还是我应该使用的另一个应用程序(因为谷歌给了我在哪里可以下载它的 apk 的链接)?
  • 我正在使用ES File Explorer。三星手机提供My Files应用。同样,您可以在 google playstore 上找到更多信息。
  • 好的。我想这就是全部了。谢谢您的宝贵时间,先生。
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 2023-03-10
    • 2023-03-05
    • 2019-01-03
    • 1970-01-01
    相关资源
    最近更新 更多