如何删除android中的绝对路径

时间:2014-02-20 05:53:28

标签: android android-listview absolute-path

我有一个列表视图,其中我从服务器加载音频文件,并点击它播放的任何音频文件。现在我有一个问题是我得到了绝对路径的完整路径,我只想要该文件的名称。

enter image description here

所以基本上在上面的图像中,您可以看到它正在使用//..../.mp3打印完整路径,我只想显示文件名。那我怎么能这样做呢。

我尝试了很多像 .getName()这样的解决方案,但文件和字符串对象却没有正常工作。

以下是我的代码。

@Override
        protected String doInBackground(String... arg0) {
            try {
                urlAudio = new URL("http://server/folder/uploadAudio");
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            ApacheURLLister lister1 = new ApacheURLLister();
            List<String> tempList = new ArrayList<String>();

            try {
                tempList = lister1.listAll(urlAudio);
            } catch (IOException e) {
                e.printStackTrace();
            }

            for (int i = 0; i < tempList.size(); i++) {
                myList.add(tempList.get(i).split("/")[tempList.get(i)
                        .split("/").length - 1]);
            }
            return null;
        }

非常感谢任何帮助。提前谢谢。

2 个答案:

答案 0 :(得分:2)

        ApacheURLLister lister1 = new ApacheURLLister();
        List<String> tempList = new ArrayList<String>();

        try {
            tempList = lister1.listAll(urlAudio);
        } catch (IOException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < tempList.size(); i++) {
            myList.add(tempList.get(i).split("/")[tempList.get(i).split("/").length-1]);
        }

更新:按照以下代码段替换整个doInBackground()方法正文......

    @SuppressWarnings("unchecked")
    @Override
    protected String doInBackground(String... arg0) {
        try {
            urlAudio = new URL("http://i-qualtech.com/Fidol/uploadAudio");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        ApacheURLLister lister1 = new ApacheURLLister();
        List<String> tempList = new ArrayList<String>();

        try {
            tempList = lister1.listAll(urlAudio);
        } catch (IOException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < tempList.size(); i++) {
            myList.add(tempList.get(i).split("/")[tempList.get(i).split("/").length-1]);
        }
        return null;
    }

答案 1 :(得分:2)

试试这个

 String filename = urlAudio.toString().substring(
            urlAudio.toString().lastIndexOf("/") + 1, urlAudio.toString().length())
相关问题