Sajith Rupasinghe
Sajith Rupasinghe

Reputation: 343

get internal directory path on galaxy note

In my application I am accessing external storage (it can be part of both internal storage or SD Card according to this) by using following method and in my Xperia V it returns the path of internal storage(Not SD card).

String root = Environment.getExternalStorageState().toString();

When I tried to run the app on a galaxy note which doesn't have a SD card mounted I found out above method doesn't return the storage path.(app is supposed to save a file if it gets the successful path). So can I know is there a method I can access external storage(which doesn't have an SD card mounted).?

Thank you

Upvotes: 0

Views: 487

Answers (3)

cYrixmorten
cYrixmorten

Reputation: 7108

private String getInternalSDPath() {
    File file = new File("/system/etc/vold.fstab");
    FileReader fr = null;
    BufferedReader br = null;

    try {
        fr = new FileReader(file);
    } catch (FileNotFoundException e) {

    }
    String path = null;
    try {
        if (fr != null) {
            br = new BufferedReader(fr);
            String s = br.readLine();
            while (s != null) {
                if (s.startsWith("dev_mount")) {
                    String[] tokens = s.split("\\s");
                    path = tokens[2]; // mount_point
                    if (Environment.getExternalStorageDirectory()
                            .getAbsolutePath().equals(path)) {
                        break;
                    }
                }
                s = br.readLine();
            }
        }
    } catch (IOException e) {

    } finally {
        try {
            if (fr != null) {
                fr.close();
            }
            if (br != null) {
                br.close();
            }
        } catch (IOException e) {

        }
    }
    return path;
}

To get the external SD card mount directory simply change:

                    if (Environment.getExternalStorageDirectory()
                            .getAbsolutePath().equals(path)) {
                        break;
                    }

To:

                    if (!Environment.getExternalStorageDirectory()
                            .getAbsolutePath().equals(path)) {
                        break;
                    }

Note: this method is reather device specific and I can use it because I know I only develop against Samsung devices which have an extra internal storage. If you want this to work for all devices you would have to make a additional changes.

Upvotes: 0

Naeem A. Malik
Naeem A. Malik

Reputation: 1025

First of all your app must have permission to write external storage, it is specified in manifest file, please see below:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Secondly, we should check if external media is mounted, Please see a working snippet from one of my Android apps it will create a directory for you on external storage if one does not exist already, otherwise return File object for the directory.

File getRecordingDirectory()
    {
        File recordingDir = null;

            // get the state of external storage
        String externalStorageState = Environment.getExternalStorageState();

        if(externalStorageState.equals(Environment.MEDIA_MOUNTED))
        {
            File extDir =
                    Environment.getExternalStorageDirectory();

            recordingDir = new File(extDir, "AudioRecorder");

            if(!recordingDir.exists())
            {
                if(!recordingDir.mkdirs())
                {
                Log.e("RECORDER", "Unable to create Silent Recorder recordings directory.");
                }
            }
        }
        return recordingDir;
    }

Upvotes: 0

kalyan pvs
kalyan pvs

Reputation: 14590

For getting the external storage path try like this..

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

It will retun the exact path of external storage

Upvotes: 1

Related Questions