Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.io.*;

import java.util.*;

import android.os.*;

public class Main {
    public static String[] getMountedVolumes() {
        final String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // we can read the External Storage...           
            //Retrieve the primary External Storage:
            final File primaryExternalStorage = Environment.getExternalStorageDirectory();

            //Retrieve the External Storages root directory:
            String externalStorageRootDir = null;
            if ((externalStorageRootDir = primaryExternalStorage.getParent()) == null) { // no parent...
                return (new String[] { "ONLY A SINGLE VOLUME HAS BEEN DETECTED!",
                        (Environment.isExternalStorageRemovable() ? "(REMOVABLE SD-CARD)" : "(INTERNAL STORAGE)")
                                + " PRIMARY STORAGE: " + primaryExternalStorage });
            } else {
                final File externalStorageRoot = new File(externalStorageRootDir);
                final File[] files = externalStorageRoot.listFiles(new FilenameFilter() {

                    @Override
                    public boolean accept(File dir, String filename) {
                        // TODO Auto-generated method stub

                        File file = new File(dir, filename);
                        if (file.isDirectory() && file.canRead() && file.canWrite() && !file.isHidden()) {
                            return true;
                        }
                        return false;
                    }

                }); //.listFiles();
                List<String> data = new ArrayList<String>();
                if (files.length > 1) {
                    data.add("MULTIPLE VOLUMES HAS BEEN DETECTED!");
                    data.add("Enumerating detected volumes . . .");
                } else {
                    data.add("ONLY A SINGLE VOLUME HAS BEEN DETECTED!");
                }

                for (final File file : files) {
                    if (file.isDirectory() && file.canRead() && file.canWrite() && !file.isHidden()
                            && (files.length > 0)) { // it is a real directory (not a USB drive)...
                        if (file.toString().equals(primaryExternalStorage.toString()))
                            data.add((Environment.isExternalStorageRemovable() ? "(REMOVABLE SD-CARD)"
                                    : "(INTERNAL Memory)") + " PRIMARY STORAGE: " + file.getAbsolutePath());
                        else {
                            data.add(((file.toString().contains("usb") || file.toString().contains("USB"))
                                    ? "MOUNTED USB"
                                    : "MOUNTED") + " STORAGE: " + file.getAbsolutePath());
                        }
                    }
                }
                return data.toArray(new String[data.size()]);
            }
        } else {
            // we cannot read the External Storage..., return null
            return null;
        }

    }
}