安卓选择mp3文件 [英] Android select mp3 file

查看:34
本文介绍了安卓选择mp3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果这已经得到回答,但我不能很好地说明我的问题.我有一个 Activity 需要您设备中的歌曲文件,当我按下按钮打开一个对话框询问您想如何打开文件(例如选择文件资源管理器)时,我想要它,然后用户将选择an mp3 file (it should be possible to enter both internal memory and external, at least those available to users, like in my Xperia V which has 2 internal partitions, and an SD Card), and when a music file is selected (.wav、.mp3、.acc 文件)在我的应用程序中加载其名称和文件路径.我该怎么做?我没有提供任何代码,因为这就是我所需要的

Sorry if this has already been answered, but I can't specify my question very well. I have an activity that needs a song file from your device, and I want it when I press a button to open a dialog to ask you how you want to open a file (like to choose a file explorer) and then the user will select an mp3 file (it should be possible to enter both internal memory and external, at least those available to users, like in my Xperia V which has 2 internal partitions, and an SD Card), and when a music file is selected (.wav, .mp3, .acc files) to load its name and file path in my app. How can I do it? I am not providing any code, because that's all I need

推荐答案

我使用以下内容来选择一个 mp3 文件:

I use the following to select an mp3 file :

Intent intent;
intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("audio/mpeg");
startActivityForResult(Intent.createChooser(intent, getString(R.string.select_audio_file_title)), REQ_CODE_PICK_SOUNDFILE);

然后你可以在 onActivityResult 中取回 Uri :

and then you can get the Uri back in onActivityResult :

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQ_CODE_PICK_SOUNDFILE && resultCode == Activity.RESULT_OK){
        if ((data != null) && (data.getData() != null)){
            Uri audioFileUri = data.getData();
            // Now you can use that Uri to get the file path, or upload it, ...
            }
        }
}

我相信选择其他类型的音频文件将是通过执行 intent.setType("audio/*) 在 MIME 类型中设置通配符的问题,尽管我还没有测试过.

I believe selecting other types of audio files would be a matter of setting a wild card in the MIME type by doing intent.setType("audio/*) although I haven't tested that.

如果这对你有用,请告诉我;-)

Please let me know if that works for you ;-)

这篇关于安卓选择mp3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆