【问题标题】:Want just the filename to show in array and not the whole path只希望文件名显示在数组中,而不是整个路径
【发布时间】:2014-05-16 16:33:19
【问题描述】:

我只需要获取我的 App 文件夹中文件的文件名,但它会提供整个路径/storage/emulated/0/AppFolder/file1.pdf。我如何只显示 File.pdf 而不是整个路径。请指教

 ArrayList<File> files = new ArrayList<File>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
File file[] =  Environment.getExternalStoragePublicDirectory("/AppFolder").listFiles();
    recursiveFileFind(file);

    setListAdapter(new ArrayAdapter<File>(ViewInspection.this,
            android.R.layout.simple_list_item_1, files));
    Toast.makeText(this, "" + files.size(), Toast.LENGTH_LONG).show();
}

public void recursiveFileFind(File[] file1) {
    int i = 0;
    String filePath = "";
    if (file1 != null) {
        while (i != file1.length) {
            filePath = file1[i].getAbsolutePath();
            files.add(file1[i]);
            if (file1[i].isDirectory()) {
                File file[] = file1[i].listFiles();
                recursiveFileFind(file);
            }else{
                            }

            i++;
            Log.d(i + "", filePath);
        }
    }
}}

【问题讨论】:

  • 你检查过docs吗?
  • 听起来要获取文件名,使用.getName()
  • @curtisLeow 我尝试了一些,但没有运气

标签: android arraylist path


【解决方案1】:

使用 File.getName() 代替 File.getAbsolutePath()

    while (i != file1.length) {
        filePath = file1[i].getName();// here
        files.add(file1[i].getName());//Updated here
         if (file1[i].isDirectory()) {
             File file[] = file1[i].listFiles();
             recursiveFileFind(file);
            }

             i++;
             //Log.d(i + "", filePath);
//Update
Toast.makeText(getActivity(), filePath, Toast.LENGTH_LONG).show();
         }

【讨论】:

  • 试过了。通过这样做,它不会显示任何内容
  • 你没有得到任何日志吗?
  • 我刚刚用 getName() 再次尝试,它显示了相同的路径输出
  • 你使用了 Log.d(i + "", filePath);在 logcat 跟踪中打印文件名。所以检查logcat的登录。否则使用 toast 来显示文件路径。
  • 是的,文件路径显示了我想要的内容,但不确定如何将其发送到适配器。适配器不太好,所以它显示在我的设备上。感谢您对此的帮助
【解决方案2】:

将 File 的 files 数组列表更改为 String 的数组列表。更改以下内容

ArrayList<String> files = new ArrayList<String>();//Updated
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
File file[] =  Environment.getExternalStoragePublicDirectory("/AppFolder").listFiles();
    recursiveFileFind(file);

    setListAdapter(new ArrayAdapter<String>(ViewInspection.this,//Updated
            android.R.layout.simple_list_item_1, files));
    Toast.makeText(this, "" + files.size(), Toast.LENGTH_LONG).show();
}

【讨论】:

    【解决方案3】:

    这里的问题:

    您创建一个File 的适配器,默认情况下ArrayAdapter 将使用它包含的对象的.toString()(在本例中为文件)。

    File.toString()

    public String toString() {
        return getPath();
    }
    

    getPath 将返回抽象路径名。

     * Converts this abstract pathname into a pathname string.  The resulting
     * string uses the {@link #separator default name-separator character} to
     * separate the names in the name sequence.
     *
     * @return  The string form of this abstract pathname
    

    由于您想获得名称,因此解决方案可能会有所不同。更快的是使用&lt;String&gt; 而不是&lt;File&gt; 并将您的代码编辑为:

    filePath = file1[i].getAbsolutePath();
    files.add(file1[i].getName());
    

    但是,如果您想从 ArrayAdapter 获取 File 实例,此解决方案无法帮助您。

    你可以:

    1. 创建类似于“包装器”类的东西,它将包含有关文件的信息,并有一个覆盖的.toString(),它将打印文件名。 像这样的包装类:

      public static class FileWrapper {
          private final File file;
          private final String fileName;
      
          public FileWrapper(File file)
          {
              this.file = file;
              this.fileName = file.getName();
          }   
      
          public File getFile() { return file; } 
      
          @Override
          public String toString()
          {
              return fileName;
          }
      }
      

      然后使用&lt;FileWrapper&gt;,您将获得正确的名称,并使用getItem(position).getFile() 获得文件实例。 警告我使用了static,因为我认为如果你将这个类声明为内部类会更好,但如果你不删除static

    2. 创建ArrayAdapter 的子类(或者,我更喜欢但BaseAdapter)并覆盖getView 以满足您的需求。我没有尝试过这个解决方案,但我发现它有点困难,因为你应该保存充气器、要使用的资源,然后充气并编辑文本。

    如果您只关心“显示文件名”,您可以进行第一次编辑,就可以了。

    【讨论】:

      猜你喜欢
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-01
      • 2022-07-15
      • 2021-10-15
      • 1970-01-01
      相关资源
      最近更新 更多