`File.listFiles()`在某些特定路径上不列出文件。

huangapple 未分类评论51阅读模式
英文:

File.listFiles() doesn't list files on some specific paths

问题

以下是您提供的代码的翻译部分:

package com.tetradev.projectpodcaster;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class DirectoryChooser extends AppCompatActivity {
    // 变量
    public String directory;

    // 视图
    ListView fileList;
    ImageButton backFolderButton;
    TextView pathTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.directory_chooser);

        // 设置默认目录。
        directory = "/storage/emulated/0";

        // 按索引分配视图。
        fileList = findViewById(R.id.listView);
        backFolderButton = findViewById(R.id.backFolderButton);
        pathTitle = findViewById(R.id.pathTitle);

        // 在启动时处理默认目录。
        setListView(getFilesFromDirectory(directory));

        // 处理ListView点击事件。
        fileList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (new File(directory + "/" + getFilesFromDirectory(directory).get(position).fileName).isDirectory()) {
                    directory = directory + "/" + getFilesFromDirectory(directory).get(position).fileName;
                    setListView(getFilesFromDirectory(directory));
                    Toast.makeText(DirectoryChooser.this, String.valueOf(directory), Toast.LENGTH_SHORT).show();
                }
            }
        });

        // 处理返回文件夹按钮。
        backFolderButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 检查是否还有父文件夹。
                if (new File(directory).getParentFile().getParentFile() != null) {
                    // 将路径设置为旧路径的父路径。
                    directory = new File(directory).getParentFile().getPath();
                    // 重置ListView
                    setListView(getFilesFromDirectory(directory));
                }
            }
        });
    }

    // 获取目录中的所有内容作为“FileClass”的列表
    public List<FileClass> getFilesFromDirectory(String path) {
        List<FileClass> tempFileList = new ArrayList<FileClass>();
        int fileCount = 0;

        // 检查文件夹是否为空。
        if (new File(path).listFiles() != null) {
            fileCount = new File(path).listFiles().length;

            for (File file : new File(path).listFiles()) {
                // 临时文件排序器
                if (file.isDirectory()) {
                    tempFileList.add(new FileClass(file.getName(), R.drawable.folderIcon));
                } else {
                    tempFileList.add(new FileClass(file.getName(), R.drawable.defaultFile));
                }
            }
        }

        // 设置pathTitle文本。
        pathTitle.setText(path + ", " + fileCount + " 个文件被找到。");

        // 将临时列表作为变量返回。
        return tempFileList;
    }

    // 将列表适配为ListView。
    public void setListView(List<FileClass> filesInDirectory) {
        FolderChooserAdapter adapter = new FolderChooserAdapter(this, filesInDirectory);
        fileList.setAdapter(adapter);
    }
}
英文:

I'm trying to create a file explorer of my own but having some problems. My code works properly, but when I try to list files on "/storage/emulated", array returns as null. I tried it on an AVD and my own phone but it's the same. Do I need some kind of privilages to access that spesific folder? I can see the contents of "/storage" and and any other path really. a quick gif

package com.tetradev.projectpodcaster;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class DirectoryChooser extends AppCompatActivity
{
    //Variables
    public String directory;

    //Views
    ListView fileList;
    ImageButton backFolderButton;
    TextView pathTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.directory_chooser);

        //Set the default directory.
        directory = &quot;/storage/emulated/0&quot;;


        //Assign views by their index.
        fileList = findViewById(R.id.listView);
        backFolderButton = findViewById(R.id.backFolderButton);
        pathTitle = findViewById(R.id.pathTitle);

        //Process the default directory on start.
        setListView(getFilesFromDirectory(directory));

        //Handling the ListView click.
        fileList.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id)
            {
                if(new File(directory + &quot;/&quot; + getFilesFromDirectory(directory).get(position).fileName).isDirectory())
                {
                    directory = directory + &quot;/&quot; + getFilesFromDirectory(directory).get(position).fileName;
                    setListView(getFilesFromDirectory(directory));
                    Toast.makeText(DirectoryChooser.this, String.valueOf(directory), Toast.LENGTH_SHORT).show();
                }

            }
        });

        //Handling the back folder button.
        backFolderButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                //Check if there is still a parent folder left.
             if(new File(directory).getParentFile().getParentFile() != null)
             {
                 //Set path as the old ones parent.
                 directory = new File(directory).getParentFile().getPath();

                 //Reset the ListView
                 setListView(getFilesFromDirectory(directory));
             }
            }
        });
    }

    //Get all contents as a list of &quot;FileClassess&quot;
    public List&lt;FileClass&gt; getFilesFromDirectory(String path)
    {
        List&lt;FileClass&gt; tempFileList = new ArrayList &lt;FileClass&gt;();

        int fileCount = 0;

        //Check if the folder is empty.
        if(new File(path).listFiles() != null)
        {
            fileCount = new File(path).listFiles().length;

            for (File file : new File(path).listFiles())
            {
                //TEMPORARY FILE SORTER
                if(file.isDirectory())
                {
                    tempFileList.add(new FileClass(file.getName(), R.drawable.folderIcon));
                }
                else
                {
                    tempFileList.add(new FileClass(file.getName(), R.drawable.defaultFile));
                }
            }
        }

        //Set the pathTitle text.
        pathTitle.setText(path + &quot;, &quot; + fileCount + &quot; files found.&quot;);

        //Return temporary list as a variable.
        return tempFileList;
    }

    //Adapt the list as a ListView.
    public void setListView(List&lt;FileClass&gt; filesInDirectory)
    {
        FolderChooserAdapter adapter = new FolderChooserAdapter(this, filesInDirectory);
        fileList.setAdapter(adapter);
    }
}

huangapple
  • 本文由 发表于 2020年7月25日 23:02:32
  • 转载请务必保留本文链接:https://java.coder-hub.com/63089866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定