我必须在Android上启动代码并启动录音机:
final Intent recordSoundIntent = new Intent
("android.provider.MediaStore.RECORD_SOUND");
String fileName = Environment.getExternalStorageDirectory() +
File.separator + UUID.randomUUID() + ".3gpp";
recordSoundIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new
File(fileName)));
startActivityForResult(Intent.createChooser(recordSoundIntent,
getString(R.string.record_sound_chooser)), INTENT_SOUND_RECORDING);
我的问题如下:
我的文件名(fileName)无效,Uri从中返回 data.getData()在我上次的测试运行中返回:content:// media / external / audio / media / 41。 但是,此文件是在我的SD卡上创建的:recording34485.3gpp。如果它 在创建声音时无法设置自定义位置 我想要这个文件的位置。
答案 0 :(得分:5)
正在解决同样的问题。无法找到直接设置位置的方法,但找到了解决方法来获取和重命名它。
获取文件名已由here处理,然后使用java.io.File.renameTo()重命名文件。您需要在清单文件中使用android.permission.WRITE_EXTERNAL_STORAGE才能使重命名生效。
这是我的测试代码示例:
public static final int REQUEST_RECORD_SOUND = 7;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, REQUEST_RECORD_SOUND);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == REQUEST_RECORD_SOUND){
String sourcePath = getRealPathFromURI(intent.getData());
File root = Environment.getExternalStorageDirectory();
String destPath = root.getPath() + File.separator + "newName.txt";
File sourceF = new File(sourcePath);
try {
sourceF.renameTo(new File(destPath));
} catch (Exception e) {
Toast.makeText(this, "Error:" + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
/**
* from:
* https://stackoverflow.com/questions/3401579/get-filename-and-path-from-uri-from-mediastore
*/
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);
}