main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="recordSound" android:text="開始錄音" /> </LinearLayout>MediaRecorderIntentActivity
public class MediaRecorderIntentActivity extends Activity { private static final String TAG = "MediaRecorderIntentActivity"; private MediaPlayer mp; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void recordSound(View v) { this.stopPlay(); Intent it = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION); this.startActivityForResult(it, 1); } @Override public void onPause() { super.onPause(); this.stopPlay(); } private void stopPlay() { if (this.mp != null) { Log.d(TAG, "停止播放"); this.mp.stop(); this.mp.release(); } } @Override public void onActivityResult(int requestCode, int resultCode, Intent it) { if (requestCode != 1) { Log.d(TAG, "不是錄音"); return; } if (resultCode != RESULT_OK) { Log.e(TAG, "錄音失敗 - " + resultCode); return; } Uri uri = it.getData(); Log.d(TAG, "錄音檔:" + uri.getPath()); this.mp = new MediaPlayer(); try { this.mp.setDataSource(this, uri); this.mp.prepare(); this.mp.start(); Log.d(TAG, "開始播放..."); } catch (IllegalArgumentException e) { Log.e(TAG, e.getMessage(), e); } catch (SecurityException e) { Log.e(TAG, e.getMessage(), e); } catch (IllegalStateException e) { Log.e(TAG, e.getMessage(), e); } catch (IOException e) { Log.e(TAG, e.getMessage(), e); } } }開啟錄音程式與錄音都沒問題,一直到呼叫 onActivityResult 後得到 resultCode 卻不是 RESULT_OK,而是令人不解的 RESULT_CANCELED(0),即使加上 android.permission.WRITE_EXTERNAL_STORAGE 權限也不行,事實上也找不到錄音檔在哪。
相關文章
我在小米2上也遇到了这个问题,在其他手机上resultCode都是-1,唯独小米2是0,奇怪
回覆刪除