|
|
|
@ -33,7 +33,6 @@ public class FileManageFragment extends Fragment {
|
|
|
|
|
private EditText path_edit;
|
|
|
|
|
private RecyclerView directoryView;
|
|
|
|
|
private List<FileItem> fileItemList;
|
|
|
|
|
private String fileNames[];
|
|
|
|
|
private File files[];
|
|
|
|
|
|
|
|
|
|
public FileManageFragment(){
|
|
|
|
@ -47,52 +46,26 @@ public class FileManageFragment extends Fragment {
|
|
|
|
|
directoryView = (RecyclerView)getView().findViewById(R.id.dir_list);
|
|
|
|
|
|
|
|
|
|
Bundle data = getArguments();
|
|
|
|
|
dir = data.getString("dir");
|
|
|
|
|
isRoot = data.getBoolean("isRoot");
|
|
|
|
|
Toast.makeText(getActivity(),dir+isRoot,Toast.LENGTH_SHORT).show();
|
|
|
|
|
|
|
|
|
|
if(isRoot){
|
|
|
|
|
loadRoot();
|
|
|
|
|
//Toast.makeText(getActivity(),"root",Toast.LENGTH_SHORT).show();
|
|
|
|
|
dir = Environment.getExternalStorageDirectory().getPath(); //获取根目录
|
|
|
|
|
}else{
|
|
|
|
|
loadDirectory(dir);
|
|
|
|
|
dir = data.getString("dir");
|
|
|
|
|
Toast.makeText(getActivity(),dir,Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
initLayout();
|
|
|
|
|
loadDirectory();
|
|
|
|
|
}
|
|
|
|
|
public String getDir(){
|
|
|
|
|
return dir;
|
|
|
|
|
}
|
|
|
|
|
private void initLayout(){
|
|
|
|
|
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL);
|
|
|
|
|
directoryView.setLayoutManager(layoutManager);
|
|
|
|
|
FileAdapter adapter = FileAdapter.getInstance(fileItemList,this);
|
|
|
|
|
adapter.setFileList(fileItemList);
|
|
|
|
|
directoryView.setAdapter(adapter);
|
|
|
|
|
}
|
|
|
|
|
private void loadRoot() {
|
|
|
|
|
//加载外部存储的根目录
|
|
|
|
|
public void loadDirectory(){
|
|
|
|
|
ActivityCompat.requestPermissions(getActivity(), new String[]{ //运行时权限
|
|
|
|
|
"android.permission.WRITE_EXTERNAL_STORAGE",
|
|
|
|
|
"android.permission.READ_EXTERNAL_STORAGE"}, 1);
|
|
|
|
|
|
|
|
|
|
String rootPath = Environment.getExternalStorageDirectory().getPath();
|
|
|
|
|
dir = rootPath;
|
|
|
|
|
path_edit.setText(dir);
|
|
|
|
|
File root = new File(rootPath);
|
|
|
|
|
files = root.listFiles();
|
|
|
|
|
if (files == null){
|
|
|
|
|
//当目录里为空时,列表为空对象
|
|
|
|
|
}else{
|
|
|
|
|
FileUtils.orderByName(files);
|
|
|
|
|
|
|
|
|
|
initDirectory();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public void loadDirectory(String path){
|
|
|
|
|
ActivityCompat.requestPermissions(getActivity(), new String[]{ //运行时权限
|
|
|
|
|
"android.permission.WRITE_EXTERNAL_STORAGE",
|
|
|
|
|
"android.permission.READ_EXTERNAL_STORAGE"}, 1);
|
|
|
|
|
path_edit.setText(path);
|
|
|
|
|
File directory = new File(path);
|
|
|
|
|
File directory = new File(dir);
|
|
|
|
|
files = directory.listFiles();
|
|
|
|
|
if (files == null){
|
|
|
|
|
//当目录里为空时,列表为空对象
|
|
|
|
@ -100,7 +73,7 @@ public class FileManageFragment extends Fragment {
|
|
|
|
|
FileUtils.orderByName(files);
|
|
|
|
|
initDirectory();
|
|
|
|
|
}
|
|
|
|
|
Toast.makeText(getActivity(),directory.canRead()+"",Toast.LENGTH_SHORT).show();
|
|
|
|
|
initLayout();
|
|
|
|
|
}
|
|
|
|
|
private void initDirectory(){
|
|
|
|
|
fileItemList = null;
|
|
|
|
@ -133,4 +106,10 @@ public class FileManageFragment extends Fragment {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private void initLayout(){
|
|
|
|
|
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL);
|
|
|
|
|
directoryView.setLayoutManager(layoutManager);
|
|
|
|
|
FileAdapter adapter = new FileAdapter(fileItemList,this);
|
|
|
|
|
directoryView.setAdapter(adapter);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|