Android Open Source - file-browser File Browser Fragment






From Project

Back to project page file-browser.

License

The source code is released under:

Copyright (c) 2014, Hyusein Gyulyustan All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are m...

If you think the Android project file-browser listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.hglstn.filebrowser;
//ww  w .  j  a v a  2  s. c o  m
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * A fragment containing file browser view.
 */
public class FileBrowserFragment extends Fragment {

  private String currentPath = null;
  private FileSystem fileSystem;

  public FileBrowserFragment() {
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.file_browser, container,
        false);
    return rootView;
  }

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    super.onCreate(savedInstanceState);

    fileSystem = new LocalFileSystem();

    View view = getView().findViewById(R.id.browser_file_list);
    ListView lv = (ListView) view;

    lv.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> adapter, View arg1,
          int position, long arg3) {
        FileInfo fileInfo = (FileInfo) adapter
            .getItemAtPosition(position);
        browse(fileInfo.getFilePath(), fileInfo.isDirectory());
      }
    });

    Button buttonUp = (Button) getView().findViewById(
        R.id.browser_button_up);
    buttonUp.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if (currentPath != null
            && FileInfo.getParent(currentPath) != null) {
          browse(FileInfo.getParent(currentPath), true);
        }
      }
    });

    // Browse to root directory
    browse("/", true);
  }

  private void browse(String filePath, boolean isDirectory) {
    try {
      if (isDirectory) {
        List<FileInfo> directoryEnteries = fileSystem.list(filePath);
        currentPath = filePath;
        TextView currentDirView = (TextView) getView().findViewById(
            R.id.browser_current_dir);
        currentDirView.setText(currentPath);
        ListAdapter directoryList = new FileInfoAdapter(getActivity(),
            R.id.file_info_name, directoryEnteries);
        ListView lv = (ListView) getView().findViewById(
            R.id.browser_file_list);
        lv.setAdapter(directoryList);
      } else {
        AlertDialog.Builder dlgAlert = new AlertDialog.Builder(
            getActivity());
        dlgAlert.setTitle("Can not browse file");
        dlgAlert.setMessage("Can not browse file:" + filePath);
        dlgAlert.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog,
                  int whichButton) {
                dialog.dismiss();
              }
            });
        dlgAlert.setCancelable(true);
        dlgAlert.create().show();
        return;
      }
    } catch (Exception e) {
      Log.i("File", "File operation fault", e);
    }
  }
}




Java Source Code List

com.hglstn.filebrowser.FileBrowserFragment.java
com.hglstn.filebrowser.FileInfoAdapter.java
com.hglstn.filebrowser.FileInfo.java
com.hglstn.filebrowser.FileSystem.java
com.hglstn.filebrowser.LocalFileSystem.java
com.hglstn.filebrowser.MainActivity.java