从SD卡文件路径路径、文件、SD

2023-09-07 04:31:45 作者:南沐成辰

我SD卡上的MP3文件。如何获得选择该文件的文件从SD卡路径?​​

动态!...一样,如果用户点击文件列表中查看变量其路径得到使用。

 公共类PlayListActivity扩展ListActivity {    //歌曲列表    公众的ArrayList<&HashMap的LT;字符串,字符串>> songsList =新的ArrayList<&HashMap的LT;字符串,字符串>>();    @覆盖    公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);        的setContentView(R.layout.playlist);        ArrayList的<&HashMap的LT;字符串,字符串>> songsListData =新的ArrayList<&HashMap的LT;字符串,字符串>>();        SongsManager PLM =新SongsManager();        //从SD卡中的所有歌曲        this.songsList = plm.getPlayList();        //通过播放列表循环        的for(int i = 0; I< songsList.size();我++){            //创建新的HashMap            HashMap的<字符串,字符串>曲= songsList.get(ⅰ);            //添加HashList到ArrayList的            songsListData.add(歌曲);        }        //添加菜单项到ListView控件        ListAdapter适配器=新SimpleAdapter(这一点,songsListData,                R.layout.playlist_item,新的String [] {SONGTITLE},新的INT [] {                        R.id.songTitle});        setListAdapter(适配器);        //选择单个ListView项        ListView控件LV = getListView();        //听单列表项的点击        lv.setOnItemClickListener(新OnItemClickListener(){            @覆盖            公共无效onItemClick(适配器视图<>母公司,观景,                    INT位置,长的id){                //获取列表项指标                INT songIndex =位置;                //开始新意图                在意向=新意图(getApplicationContext()                        AndroidBuildingMusicPlayerActivity.class);                //发送到songIndex PlayerActivity                in.putExtra(songIndex,songIndex);                的setResult(100,上);                //关闭PlayListView                完();            }        });    }} 

解决方案

我想你想从一个打开文件对话框的文件,请查看以下链接

参考:选择文件对话框

您可以得到SD卡的路径下面命令的帮助:

 字符串BASEDIR = Environment.getExternalStorageDirectory()getAbsolutePath()。字符串文件名=myFile.mp3; 
SD卡删除文件后如何恢复原来的数据

所以,路径是

 字符串路径= BASEDIR +/你的文件夹(S)/+文件名; 

参考是:Android如何使用Environment.getExternalStorageDirectory()

或者你可以尝试:

 新的文件(到/ mnt / external_sd /文件夹(S)../ file.mp3); //从SD卡上的文件 

参考:How我可以外接SD卡路径Android 4.0以上版本?

I have mp3 files on sd card . how to get the path of file from sd card on selecting the file?

dynamically !...like if user click on file in list view its path get in variable for use.

public class PlayListActivity extends ListActivity {
    // Songs list
    public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playlist);    
        ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();    
        SongsManager plm = new SongsManager();
        // get all songs from sdcard
        this.songsList = plm.getPlayList();

        // looping through playlist
        for (int i = 0; i < songsList.size(); i++) {
            // creating new HashMap
            HashMap<String, String> song = songsList.get(i);

            // adding HashList to ArrayList
            songsListData.add(song);
        }    
        // Adding menuItems to ListView
        ListAdapter adapter = new SimpleAdapter(this, songsListData,
                R.layout.playlist_item, new String[] { "songTitle" }, new int[] {
                        R.id.songTitle });

        setListAdapter(adapter);

        // selecting single ListView item
        ListView lv = getListView();
        // listening to single listitem click
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting listitem index
                int songIndex = position;

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        AndroidBuildingMusicPlayerActivity.class);
                // Sending songIndex to PlayerActivity
                in.putExtra("songIndex", songIndex);
                setResult(100, in);
                // Closing PlayListView
                finish();
            }
        });    
    }
}

解决方案

I think you want to get file from a file open dialog, check out the below link

Reference: Choose File Dialog

You can get path of SD card with the help of command below:

String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "myFile.mp3";

So path will be

String path  = baseDir + "/your folder(s)/" + fileName;

Reference is: Android how to use Environment.getExternalStorageDirectory()

Or you can try:

new File("/mnt/external_sd/your folder(s)../file.mp3");//get a file from SD card

Reference: How can I get external SD card path for Android 4.0+?