有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么openFd(文件名)不能使用字符串?

private void startSound(String filename) throws IOException{
    AssetFileDescriptor afd = getAssets().openFd(filename);
    MediaPlayer player = new MediaPlayer();
    player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
    player.prepare();
    player.start();
}

@Override
public void onClick(View v) {
    switch(v.getId()) {
case R.id.addForLearning:

        break;
    case R.id.music:

        String words = mainDataTextView.getText().toString();

        String con = "voice/"+words+".mp3";

        try {
            startSound(con);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        break;

为什么String in openFd(filename)不起作用?我得到了FileNotFoundException。如果我键入openFd("voice/song.mp3")所有的都可以,但它不适合我


共 (1) 个答案

  1. # 1 楼答案

    我敢打赌,你从文本视图中得到的字符串并没有删减空格

    试试看

    String words = mainDataTextView.getText().toString();
    String trimmedWords = words.trim();
    String con = "voice/"+trimmedWords+".mp3";
    

    此外,如果这是不正确的,您能打印words包含的内容吗