2

我在 sd 卡上有 mp3 文件。选择文件时如何从sd卡获取文件路径?

动态!...就像用户在列表视图中单击文件时,它的路径进入变量以供使用。

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();
            }
        });    
    }
}
4

5 回答 5

7

我认为您想从文件打开对话框中获取文件,请查看以下链接

参考:选择文件对话框

您可以借助以下命令获取 SD 卡的路径:

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

所以路径将是

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

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

或者您可以尝试:

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

参考:如何获取 Android 4.0+ 的外部 SD 卡路径?

于 2012-12-04T16:56:10.607 回答
0

您可以使用getExternalStorageDirectory()获取 sd 卡的根作为File对象

于 2012-12-04T16:48:13.890 回答
0

要获取文件,您应该使用类似这样的东西

public static byte[] readFile (String file, Context c) throws IOException {
    File f = new
    File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC), file);
    if (f.exists())
        return readFile(f);
    else return new byte[0];
}

然后你可以用你得到的字节解码文件。

于 2012-12-10T16:33:48.380 回答
0

我认为此代码将与您一起使用...尝试一下并按照我的结果

公共类 SongsManager {

final String MEDIA_PATH = new String("/sdcard/");
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

// Constructor
public SongsManager(){

}

/**d
 * Function to read all mp3 files from sdcard
 * and store the details in ArrayList
 * */
public ArrayList<HashMap<String, String>> getPlayList(){
        File List[] = Environment.getExternalStorageDirectory().listFiles();  

    File home;

    int index=0;

        while(index<List.length)
        {
            home = new File(List[index].getAbsolutePath().toString());
            if (home.listFiles(new FileExtensionFilter()).length > 0) {
            for (File file : home.listFiles(new FileExtensionFilter())) {

                HashMap<String, String> song = new HashMap<String, String>();
                song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
                song.put("songPath", file.getPath());

                // Adding each song to SongList
                songsList.add(song);
            }
            index++;
        }
    }
    // return songs list array
    return songsList;
}



/**
 * Class to filter files which are having .mp3 extension
 * */
class FileExtensionFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".mp3") || name.endsWith(".MP3"));
    }
}

}

于 2012-12-24T19:43:54.033 回答
-1

4天后我得到了解决方案。在 Android(Java) 中给出 File 类的路径时请注意以下几点:

  1. 使用路径进行内部(在 android 中调用外部)存储

    String path="/storage/sdcard0/myfile.txt";
    
  2. 在存储卡中

    path="/storage/sdcard1/myfile.txt";
    
  3. 在清单文件中提及权限。

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    
  4. 首先检查文件长度以确认。

  5. 检查 ES 文件资源管理器中有关 sdcard0 和 sdcard1 的路径是否相同,否则......例如

    File file=new File(path);
    long=file.length(); //in Bytes
    
于 2016-09-12T07:07:59.787 回答