0

我是Android開發的新手,我想知道是否可以保存已轉換的已轉換語音文本文件Google語音識別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; 
     } 

    } 
} 

請原諒我使用的代碼格式的殘疾(I」 m仍然在習慣它)。

+0

我沒有得到你真正想要的?將語音到文本數據存儲到存儲器或將用戶的語音存儲到存儲器 – ELITE

+0

我真正想要的是兩者。它是這樣的,我的應用程序有兩個庫,它們是: 1.)錄製的語音(由API錄製的語音) 2.)文本文件(由API轉換的文件) 因此,實際上將輸出數據存儲到用戶的設備存儲器中。 – Jondy

回答

0

試試這個代碼寫在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_STORAGEWRITE_EXTERNAL_STORAGE許可,不得以AndroidManifest.xml文件。

參考this問題

+0

我應該在方法onCreate()下創建這個方法嗎?如果你不介意,我還有一個問題。如何在使用應用程序時查看外部存儲器中保存的文件? – Jondy

+0

你不能在另一個方法中聲明一個方法。所以你必須在activity的onCreate()之外創建這個方法。你可以使用任何文件瀏覽器打開文件。 – ELITE

+0

好的,謝謝你的信息。順便說一句,如果你告訴我關於文件瀏覽器的更多信息,你介意嗎?我通過谷歌搜索它,我似乎並沒有很好地理解它。我是否應該使用方法或其他應用程序調用它(因爲谷歌給了我鏈接哪裏可以下載它的apk)? – Jondy

0

如果您需要保存的文字,數字或數據,有很多方面:

  • 共享偏好(在鍵值對商店中的私人原始數據)
  • 內部存儲(存儲私人數據在設備存儲器)
  • 外部存儲(存儲公共的共享外部存儲數據)
  • SQLite數據庫(存儲結構在私有數據庫中的數據)上噸
  • 網絡連接(存儲數據他的網絡與自己的網絡服務器)

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

相關問題