1

How can I access/read the Android data Directory? I tried

File file = new File(Environment.getDataDirectory().toString());
Log.w("File", "read? "+file.canRead());
Log.w("File", "write?"+file.canWrite());

but both return false. How can I fix this? I already added the external permissions:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
frogatto
  • 28,539
  • 11
  • 83
  • 129
user2660369
  • 245
  • 1
  • 4
  • 11
  • On which version of os are you testing?. You will need run time permissions on marshmallow – Raghunandan Jan 10 '16 at 15:32
  • see this : [http://stackoverflow.com/a/21230946/2425682](http://stackoverflow.com/a/21230946/2425682) – alias_boubou Jan 10 '16 at 15:34
  • Possible duplicate of [getFilesDir() vs Environment.getDataDirectory()](http://stackoverflow.com/questions/21230629/getfilesdir-vs-environment-getdatadirectory) – starkshang Jan 10 '16 at 15:35

1 Answers1

0

The problems is that Environment.getDataDirectory() returns the system data directory path, not the one for your app. That's why you don't have permission to read or write it. There's essentially two ways to do it:

  • Call getFilesDir() instead. Something like this: File file = getFilesDir() Note that this must be called from an Activity or Service Context.

  • Append your package name to the path returned by Environment.getDataDirectory(), like this: File file = new File( Environment.getDataDirectory() + "com.mycompany.myapp/")

frogatto
  • 28,539
  • 11
  • 83
  • 129
Jonas Czech
  • 12,018
  • 6
  • 44
  • 65