我正在尝试使用以下代码在 Android 2.2.1(设备 Samsung Galaxy POP)上调用录音机:
private static final int ACTIVITY_RECORD_SOUND = 1;
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, ACTIVITY_RECORD_SOUND);
这将成功调用记录器。在我的 Activity 结果中,我执行以下操作:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case ACTIVITY_RECORD_SOUND:
data.getDataString();
break;
}
}
}
完成录音后,我按下录音机,它按预期将控制权返回给 onActivityResult 方法,但我的 resultCode 始终为 0(即 Activity.RESULT_CANCELED)并且我的数据为空。我在这里错过了什么吗?请帮我解决这个问题。这适用于模拟器,但不适用于设备。提前致谢。
最佳答案
这对我有用:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode) {
case Flashum.TAKE_MUSIC:
case Flashum.TAKE_VOICE:
if (resultCode == Activity.RESULT_OK)
{
Log.i(Flashum.LOG_TAG, "onActivityResult got new music");
Bundle extras = data.getExtras();
try {
Uri u = data.getData();
String imageUri;
try {
imageUri = getRealPathFromURI(u);
} catch (Exception ex) {
imageUri = u.getPath();
}
File file = new File(imageUri);
FragmentFlash fragmentFlash = (FragmentFlash)mTabsAdapter.getFragment("flash");
if (fragmentFlash != null)
fragmentFlash.gotMusic(file.getPath());
} catch (Exception ex) {
String s = ex.toString();
Log.i(Flashum.LOG_TAG, "onActivityResult " + s);
}
}
else
{
Log.i(Flashum.LOG_TAG, "onActivityResult Failed to get music");
}
break;
}
}
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
关于android - 调用录音机并获取结果文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6746979/