File path from sd card

18,656

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+?

Share:
18,656
Rafi ullah
Author by

Rafi ullah

Updated on June 04, 2022

Comments

  • Rafi ullah
    Rafi ullah almost 2 years

    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();
                }
            });    
        }
    }
    
  • Rafi ullah
    Rafi ullah over 11 years
    i have hard coded the values. now i need to get from the user.
  • Johnny Five
    Johnny Five over 4 years
    External might be not an SD card. ContextCompat.getExternalFilesDirs(context, null), for example return array of external storages. One of them "/storage/emulated/" other might be "/storage/86C4-1A0F/". SD card is the second one with symbols and dash. To know which one is SD card you can use Environment.isExternalStorageRemovable(file) which return true if it's SD (if it is the only removable thing in your device of course)