0

我正在阅读 Android Developer's Cookbook。我在“食谱:使用语音转文本启动活动以获得结果”。当我运行应用程序时,我得到了抱歉!/强制关闭 toast,我在 LogCat 中收到此错误:无法打开堆栈跟踪文件'/data/anr/traces.txt':权限被拒绝。

谷歌搜索了一段时间没有运气。我敢肯定这是显而易见的。这是我的代码:

package com.jarednielsen.ADC;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ADC02Activity extends Activity {

private static final int RECOGNIZER_EXAMPLE = 1001;
private TextView tv;

/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tv = (TextView) findViewById(R.id.text_result);

    Button startButton = (Button) findViewById(R.id.trigger);
    startButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say a word or phrase\nand it will show as text");
            startActivityForResult(intent, RECOGNIZER_EXAMPLE);
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if (requestCode==RECOGNIZER_EXAMPLE && resultCode == RESULT_OK){
        ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

        tv.setText(result.toString());
    }

    super.onActivityResult(requestCode,  resultCode, data);
}
}
4

1 回答 1

0

我认为您需要在清单文件中使用这些权限才能进行语音识别:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
于 2012-12-07T04:59:38.990 回答