0

I'm trying to read a file from a folder on my Galaxy S4. When I place the file in the root directory, I can access it without problems:

File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "MyFile"); 

But I want to put my file in a subdirectory. When I make a folder 'A', place my file inside of it and try to access it with:

File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar + "A", "MyFile"); 

I can't read it. Do I need some kind of permission and what's the logic behind that, when I can access the root?

4
  • 1
    What permissions do you have? Only the read permission correct? Commented Feb 26, 2015 at 10:00
  • I don't have any actually. But do i need them, if I am allowed to read from the root? Commented Feb 26, 2015 at 10:02
  • 1
    Go have a look at this part of the Manifest.permission document developer.android.com/reference/android/… Commented Feb 26, 2015 at 10:05
  • Yes this was the case. If I want to dig in sub-directories, I need the READ_EXTERNAL_STORAGE permission. Thanks man! Commented Feb 26, 2015 at 10:11

1 Answer 1

1

You can read here:

http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()

that access to this folder is not recomended:

Applications should not directly use this top-level directory, in order to avoid polluting the user's root namespace. Any files that are private to the application should be placed in a directory returned by Context.getExternalFilesDir, which the system will take care of deleting if the application is uninstalled.

Also, permission is required:

Writing to this path requires the WRITE_EXTERNAL_STORAGE permission, and starting in read access requires the READ_EXTERNAL_STORAGE permission, which is automatically granted if you hold the write permission.

[edit]

Also, since you are using s4 - which is probably 4.4+ device, you should know that since KitKat Google has disallowed write access on removable media, you can only read it, or write it to your application folder:

http://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html

As a result, apps can read files on removable media using the various undocumented and unsupported tricks for finding removable media. However, apps cannot write to or otherwise modify such removable storage. Note that device manufacturers themselves may have ways of dealing with this, but ordinary app developers do not.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.