【问题标题】:How to remove absolute path in android如何在android中删除绝对路径
【发布时间】:2014-02-20 05:53:28
【问题描述】:

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

所以基本上在上图中,您可以看到它正在打印带有 //..../.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;
        }

任何帮助将不胜感激。提前谢谢你。

【问题讨论】:

  • 使用 string.split() 方法
  • @user88,我必须在哪里使用这个 string.split?
  • 可以上传listAll()方法的代码吗?
  • @Kedarnath,listAll() 是默认方法,无需明确定义,我使用了 apache ivy 库来处理它。
  • 谁投反对票,请说明原因。

标签: android android-listview absolute-path


【解决方案1】:

试试这个

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

【讨论】:

  • 我应该在哪里使用这个?
  • 你想在哪里显示或存储文件名,你可以使用这个。
  • @InnocentKiller 在List&lt;String&gt; myList中添加路径的地方使用这一行
  • lastIndrxof 显示错误,错误为 The method lastIndexOf(String) is undefined for the type URL。这里 urlAudio 被声明为 URL
  • @InnocentKiller 让它成为urlAudio.toString.lastIndexOf("/")
【解决方案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]);
        }

更新:用下面的代码 sn-p...替换你的整个 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;
    }

【讨论】:

  • 好的,等一下,我会尽力告诉你的。
  • 还是一样,没有得到准确的结果,只显示完整路径。
  • 那么你的问题出在其他任何地方。
  • 可能是,但我不知道到底是什么问题以及出处。
  • 你有两个AsyncTask...更改它们。
猜你喜欢
  • 1970-01-01
  • 2021-07-02
  • 2018-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多