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

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

问题描述

很抱歉,如果已经回答了这个问题,但是我不能很好地说明我的问题. 我有一个活动需要您设备中的歌曲文件,当我按下按钮以打开对话框询问您如何打开文件(例如选择文件浏览器)时,我想要它,然后用户将选择一个mp3文件(应该可以同时输入内部存储器和外部存储器,至少可以供用户使用,例如在我的Xperia V中具有两个内部分区和一个SD卡),以及选择了音乐文件时(.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 ;-)

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

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