Search code examples
androidandroid-file

File explorer , show only video files


I want to adapt the project to a player. How can I make it look just video files. other files like txt, etc. apk not open them or not to show them. Thank you.

code:

public class MainActivity extends ListActivity {

private List<String> item = null;
private List<String> path = null;
private String root;
private TextView myPath;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myPath = (TextView)findViewById(R.id.path);

    root = Environment.getExternalStorageDirectory().getPath();

    getDir(root);
}

private void getDir(String dirPath)
{
    myPath.setText("Location: " + dirPath);
    item = new ArrayList<String>();
    path = new ArrayList<String>();
    File f = new File(dirPath);
    File[] files = f.listFiles();

    if(!dirPath.equals(root))
    {
        item.add(root);
        path.add(root);
        item.add("../");
        path.add(f.getParent());    
    }

    for(int i=0; i < files.length; i++)
    {
        File file = files[i];

        if(!file.isHidden() && file.canRead()){
            path.add(file.getPath());
            if(file.isDirectory()){
                item.add(file.getName() + "/");
            }else{
                item.add(file.getName());
            }
        }   
    }

    ArrayAdapter<String> fileList =
            new ArrayAdapter<String>(this, R.layout.row, item);
    setListAdapter(fileList);   
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    File file = new File(path.get(position));

    if (file.isDirectory())
    {
        if(file.canRead()){
            getDir(path.get(position));
        }else{
            new AlertDialog.Builder(this)
                .setIcon(R.drawable.ic_launcher)
                .setTitle("[" + file.getName() + "] folder can't be read!")
                .setPositiveButton("OK", null).show();  
        }   
    }else {
        new AlertDialog.Builder(this)
                .setIcon(R.drawable.ic_launcher)
                .setTitle("[" + file.getName() + "]")
                .setPositiveButton("OK", null).show();

      }
}

}

I do not know how you can change to show only a certain type of resources from sdcard.

Thank you.


Solution

  • You could check if the fileextension is something like .mp4 or .avi etc...

    if(!file.isHidden() && file.canRead()) {
        path.add(file.getPath());
        if(file.isDirectory()){
            item.add(file.getName() + "/");
        }else{
            if(isVideo(file)){            
                item.add(file.getName());
            }
        }   
    }
    

    where isVideo is something like

    bool isVideo(File file){
        String filename = file.getName().toLowerCase();
        String extension =  filename.substring(filename.lastIndexOf("."), filename.length());
    
        switch (extension) {
                case ".3gp":
                case ".mpg":
                case ".mpeg":
                case ".mpe":
                case ".mp4":
                case ".avi":
                    return true;
                default:
                    return false;
        }
    }