Environment.getExternalStorageDirectory()。listFiles()返回null

时间:2015-11-24 10:57:45

标签: android

我的应用程序中需要一个文件选择器,在尝试过没有工作的第三方库(没有内容)之后,我试图自己实现它,并且我有相同的结果。

最奇怪的是

Environment.getExternalStorageDirectory().listFiles() 

返回null

这是我的代码,

public class FilePickerActivity extends AppCompatActivity {

    FilePickerActivity mActivity = this;
    FilePickerAdapter mAdapter;

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

        ListView listView = (ListView) findViewById(R.id.list);
        mAdapter = new FilePickerAdapter(this, new File("/"));//Environment.getExternalStorageDirectory());
        listView.setAdapter(mAdapter);
    }

    private class FilePickerAdapter extends ArrayAdapter<File> {

        ArrayList<File> mDataset = new ArrayList<>();
        Context context;
        File rootFile;

        public void explore(File file){
            rootFile = file;
            refresh();
        }

        public void refresh(){

            mDataset.clear();
            File[] list = rootFile.listFiles();

            if (list != null) {
                for (File file : list) {
                    if (!file.isHidden()) {
                        mDataset.add(file);
                    }
                }
            }
            notifyDataSetChanged();
        }

        public boolean getHigher(){
            rootFile = rootFile.getParentFile();
            if(rootFile == null ){
                return false;
            }
            refresh();
            return true;
        }

        public FilePickerAdapter(Context context,
                                  File rootFile) {
            super(context, R.layout.aaaaaaaaa_file_picker_list_item, new ArrayList<File>());
            this.context = context;
            this.rootFile = rootFile;
            refresh();
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getCount() {
            return mDataset.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null){
                convertView = LayoutInflater.from(context).inflate(R.layout.aaaaaaaaa_file_picker_list_item, parent, false);
            }

            final File itemFile = mDataset.get(position);
            final String itemString = itemFile.getName();

            ((TextView) convertView.findViewById(R.id.textView)).setText(itemString);
            ImageView iv = (ImageView) convertView.findViewById(R.id.iv);

            if(itemFile.isDirectory()){
                iv.setImageResource(R.drawable.ic_attachment_24dp);
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        explore(itemFile);
                    }
                });
            } else {
                iv.setImageResource(R.drawable.ic_arrow_vector);
            }

            return convertView;
        }
    }

    @Override
    public void onBackPressed() {
        if(!mAdapter.getHigher())
            super.onBackPressed();
    }
}

谢谢,

3 个答案:

答案 0 :(得分:0)

看看您的SD卡已安装;

 Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())

答案 1 :(得分:0)

面对同样的问题。这可能是由于很多原因,但我的权限是不正确的。 对于23以上的API,您必须添加运行时权限以及Manifest。 希望它可以帮到某人。

答案 2 :(得分:0)

在清单文件中添加以下行:

android:requestLegacyExternalStorage="true"